
"""Phishing.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/126__574uNCBltWIvSvBxoB--lqW9YS2J
"""

from google.colab import drive
drive.mount('/content/drive')

# Commented out IPython magic to ensure Python compatibility.
# Importing necessary libraries

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# %matplotlib inline

fishing = pd.read_csv("drive/MyDrive/Dataset/Phishing.csv")
fishing.head(5)

fishing = fishing.drop(['id'], axis = 1)

fishing.shape

fishing.isnull().sum()

# This dataset contains 48 features extracted from 5000 phishing webpages and 5000 legitimate webpages.

fishing.describe()

# Classes to predict from.

fishing['CLASS_LABEL'].value_counts()

# Finding more from the data

fishing_class = fishing.groupby('CLASS_LABEL')
fishing_class['NoHttps'].value_counts()

fishing_class['UrlLength'].mean()

fishing_class['NumPercent'].mean()

fishing_class['NumAmpersand'].mean()

fishing_class['NumHash'].mean()

fishing_class['IpAddress'].value_counts()

plt.figure(figsize=(30, 30))
sns.heatmap(fishing.corr(),annot=True,cmap='viridis',linewidths=.5)

fishing.columns

subset_fishing = fishing[['NumDots', 'PathLevel', 'NumDash', 'NumSensitiveWords', 'PctExtHyperlinks',
       'PctExtResourceUrls', 'InsecureForms', 'PctNullSelfRedirectHyperlinks', 'FrequentDomainNameMismatch',
       'SubmitInfoToEmail', 'IframeOrFrame', 'CLASS_LABEL']]

subset_fishing.head()

subset_fishing.shape

y = subset_fishing['CLASS_LABEL']
X = subset_fishing.drop(['CLASS_LABEL'], axis = 1)

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier

Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.2, random_state = 42)

random_model = RandomForestClassifier(n_estimators=250, n_jobs = -1)

#Fit
random_model.fit(Xtrain, ytrain)

y_pred = random_model.predict(Xtest)

#Checking the accuracy
random_model_accuracy = round(random_model.score(Xtrain, ytrain)*100,2)
print(round(random_model_accuracy, 2), '%')

random_model_accuracy1 = round(random_model.score(Xtest, ytest)*100,2)
print(round(random_model_accuracy1, 2), '%')

# Save the trained model as a pickle string.
import pickle 

saved_model = pickle.dump(random_model, open('drive/MyDrive/Dataset/Models/Phishing.pickle','wb'))

