Commit 6939f1c1 authored by Ashen Udayanga Sudugala's avatar Ashen Udayanga Sudugala

Merge branch 'Sakindu' into 'master'

Sakindu

See merge request !9
parents 9a6723f2 2e437b26
This source diff could not be displayed because it is too large. You can view the blob instead.
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 22 19:14:10 2020
@author: Sakindu Udagedara
"""
#Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Importing tha dataset
dataset = pd.read_csv('Sample.csv')
X = dataset.iloc[:,[1,3]].values
Y = dataset.iloc[:,5].values
#taking care of missing data
#from sklearn.preprocessing import Imputor
#imputor = Imputor (missing_values = 'NaN', strategy = 'mean' , axis = 0)
#imputor.fix(X[:,])
#encoding catagarical data
from sklearn.preprocessing import LabelEncoder
lebekencoder_X = LabelEncoder()
#X[:, 0] = lebekencoder_X.fit_transform(X[:, 0])
#X[:, 2] = lebekencoder_X.fit_transform(X[:, 2])
lebekencoder_Y = LabelEncoder()
Y = lebekencoder_Y.fit_transform(Y)
#spliting data into tarin set and test set
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.3, random_state = 0)
#feature scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.fit_transform(X_test)
# Fiting SVM to the Traing set
from sklearn.svm import SVC
classifier = SVC(kernel = 'linear', random_state = 0)
classifier.fit(X_train, Y_train)
# Predicting the Test set results
y_pred = classifier.predict(X_test)
#makeing the confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(Y_test, y_pred)
#Visualizing the Training set results
from matplotlib.colors import ListedColormap
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.contour(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(),X2.max())
plt.ylim(X2.min(),X1.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('SVM (Training set)' )
plt.xlabel('Source Port')
plt.ylabel('Destination Port')
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