0% found this document useful (0 votes)
11 views2 pages

KNN SVM

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

KNN SVM

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pandas as pd

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix, ConfusionMa
data = pd.read_csv("emails.csv")
data

data = data.drop('Email No.', axis=1)


data.shape
(5172, 3001)
data.describe()

data.info()

data['Prediction'].value_counts()
Prediction
0 3672
1 1500
Name: count, dtype: int64

X = data.drop('Prediction', axis = 1)
y = data['Prediction']

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)
from sklearn.neighbors import KNeighborsClassifier
neigh = KNeighborsClassifier(n_neighbors = 2)
neigh.fit(X_train, y_train)

y_pred = neigh.predict(X_test)
neigh.score(X_train, y_train)
neigh.score(X_test, y_test)
0.8966183574879227

print("Confusion Matrix: ")


cm = confusion_matrix(y_test, y_pred)
cm
Confusion Matrix:
array([[701, 34],
[ 73, 227]], dtype=int64)
mat = ConfusionMatrixDisplay(confusion_matrix = cm)
mat.plot()
plt.show()

print(classification_report(y_test, y_pred))

print("accuracy_score: ")
accuracy_score(y_test, y_pred)

print("precision_score: ")
precision_score(y_test, y_pred)

print("recall_score: ")
recall_score(y_test, y_pred)

print("Error: ")
1-accuracy_score(y_test, y_pred)

from sklearn.svm import SVC


SVM = SVC(gamma = 'auto')
SVM.fit(X_train, y_train)

y_pred = SVM.predict(X_test)
SVM.score(X_train, y_train)
SVM.score(X_test, y_test)
0.9120772946859903
print("Confusion Matrix: ")
cm = confusion_matrix(y_test, y_pred)
cm
Confusion Matrix:
mat = ConfusionMatrixDisplay(confusion_matrix = cm)
mat.plot()
plt.show()

You might also like