Commit 720c6e7b authored by Haritha Chanuka's avatar Haritha Chanuka

Replace IT17106702.py

parent f8cec5dc
...@@ -2,42 +2,50 @@ ...@@ -2,42 +2,50 @@
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import pandas as pd import pandas as pd
from sklearn.impute import SimpleImputer
# Importing the dataset # Importing the dataset
dataset = pd.read_csv('packet_data.csv') dataset = pd.read_csv("/Users/harithachanuka/Documents/SLIIT/Research/Harry/Wanheda_Server/MobileBotNet/packet_data.csv", encoding='latin-1')
X = dataset.iloc[:, :-1] .values X = dataset.iloc[:, [1,3]].values
y = dataset.iloc[:, 5].values y = dataset.iloc[:, 5].values
#Encoding categorical data # datasetnew = pd.read_csv("/Users/harithachanuka/Documents/SLIIT/Research/Implementation/new.csv", encoding='latin-1')
# Xnw = datasetnew.iloc[:, :5].values
#Encoding categorical data-ForX
from sklearn.preprocessing import LabelEncoder, OneHotEncoder from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer from sklearn.compose import ColumnTransformer
labelencoder_X_T = LabelEncoder()
X[:, 0] = labelencoder_X_T.fit_transform(X[:, 0])
labelencoder_X_S = LabelEncoder() labelencoder_X_S = LabelEncoder()
X[:, 1] = labelencoder_X_S.fit_transform(X[:, 1]) X[:, 0] = labelencoder_X_S.fit_transform(X[:, 0])
labelencoder_X_D = LabelEncoder() #labelencoder_X_D = LabelEncoder()
X[:, 2] = labelencoder_X_D.fit_transform(X[:, 2]) # X[:, 2] = labelencoder_X_D.fit_transform(X[:, 2])
labelencoder_X_P = LabelEncoder() labelencoder_X_P = LabelEncoder()
X[:, 3] = labelencoder_X_P.fit_transform(X[:, 3]) X[:, 1] = labelencoder_X_P.fit_transform(X[:, 1])
ct = ColumnTransformer([("Source", OneHotEncoder(), [1])], remainder='passthrough') # ct = ColumnTransformer([("Source", OneHotEncoder(), [1])], remainder = 'passthrough')
X = ct.fit_transform(X).toarray() # X = ct.fit_transform(X).toarray()
ct2 = ColumnTransformer([("Destination", OneHotEncoder(), [72])], remainder='passthrough') # ct2 = ColumnTransformer([("Destination", OneHotEncoder(), [72])], remainder = 'passthrough')
X = ct2.fit_transform(X) # X = ct2.fit_transform(X)
ct3 = ColumnTransformer([("Protocol", OneHotEncoder(), [118])], remainder='passthrough') # ct3 = ColumnTransformer([("Protocol", OneHotEncoder(), [118])], remainder = 'passthrough')
X = ct3.fit_transform(X) # X = ct3.fit_transform(X)
labelencoder_y = LabelEncoder() labelencoder_y = LabelEncoder()
y = labelencoder_y.fit_transform(y) y = labelencoder_y.fit_transform(y)
#Splitting the data set into the training set and test set # Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
# print(X_train)
# print(X_test)
# print(y_train)
# print(X_train)
#Feature scaling # Feature Scaling
from sklearn.preprocessing import StandardScaler from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler() sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train) X_train = sc_X.fit_transform(X_train)
X_test = sc_X.fit_transform(X_test) X_test = sc_X.transform(X_test)
# Fitting classifier to the Training set # Fitting classifier to the Training set
from sklearn.naive_bayes import GaussianNB from sklearn.naive_bayes import GaussianNB
...@@ -47,42 +55,8 @@ classifier.fit(X_train, y_train) ...@@ -47,42 +55,8 @@ classifier.fit(X_train, y_train)
# Predicting the Test set results # Predicting the Test set results
y_pred = classifier.predict(X_test) y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred) cm = confusion_matrix(y_test, y_pred)
# Visualising the Training set results #
from matplotlib.colors import ListedColormap \ No newline at end of file
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('BotNet (Training set)')
plt.xlabel('Time Source Dest & Protocol')
plt.ylabel('Malicious Or Not')
plt.legend()
plt.show()
# Visualising the Test set results
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Naive Bayes (Test set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment