-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML5
More file actions
161 lines (120 loc) · 4.48 KB
/
ML5
File metadata and controls
161 lines (120 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pandas as pd
import seaborn as sns
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import ConfusionMatrixDisplay, accuracy_score, precision_score
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score, precision_score,classification_report,confusion_matrix
from sklearn.svm import SVC
df=pd.read_csv(r'C:\Users\user\Documents\AB_ML\Assign 5\emails.csv')
print(df.shape)
print(df.head())
#input data
x = df.drop(['Email No.','Prediction'], axis =1)
print(x.shape)
y = df['Prediction']
print(y.shape)
#checking balance of spam and not spam
print(y.value_counts())
#Feature Scaling
scaler = MinMaxScaler()
x_scaled = scaler.fit_transform(x)
print(x_scaled)
#Cross validation
x_train, x_test, y_train, y_test = train_test_split(x_scaled,y,random_state = 0, test_size = 0.25)
print(x_scaled.shape)
print(x_train.shape)
print(x_test.shape)
#create the object
knn = KNeighborsClassifier(n_neighbors=7)
#Train the algorithm
knn.fit(x_train,y_train)
#predict on test data
y_pred = knn.predict(x_test)
cm_display = ConfusionMatrixDisplay.from_predictions(y_test,y_pred)
print(y_test.value_counts())
#print accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%")
#print precision
precision = precision_score(y_test, y_pred)
print(f"Precision: {precision * 100:.2f}%")
#print accuracy and precision
a = classification_report(y_test, y_pred)
print("report is : ", a)
#svm
svm_classifier = SVC(kernel='linear')
svm_classifier.fit(x_train, y_train)
# Predictions on test dataset
y_pred = svm_classifier.predict(x_test)
print("Classfication report")
print(classification_report(y_test, y_pred))
print("\n")
print("Confusion Matrix")
print(confusion_matrix(y_test, y_pred))
print("\n")
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)*100
print("Accuracy:",accuracy,"%")
# Calculate precision
precision = precision_score(y_test, y_pred)*100
print("Precision:", precision, "%")
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report, accuracy_score
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="joblib")
import os
os.environ["LOKY_MAX_CPU_COUNT"] = "2"
# Load the dataset
file_path = 'emails.csv'
data_frame = pd.read_csv(file_path)
print("Dataset : \n", data_frame)
data_frame['Prediction'] = data_frame['Prediction'].replace({0: 'spam', 1: 'ham'})
# Prepare features and labels
X = data_frame.iloc[:, 1:-1].values
Y = data_frame.iloc[:, -1].values
Y = np.where(Y == 'spam', 1, 0)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=90)
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
X_train_2D = X_train[:, :2]
X_test_2D = X_test[:, :2]
svm = SVC(kernel='rbf', random_state=0, gamma='scale')
svm.fit(X_train_2D, Y_train)
y_pred_svm = svm.predict(X_test_2D)
print("\n\n\t\t\tClassification Report (SVM)")
print(classification_report(Y_test, y_pred_svm))
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, Y_train)
y_pred_knn = knn.predict(X_test)
print("\n\n\t\t\tClassification Report (kNN)")
print(classification_report(Y_test, y_pred_knn))
def plot_decision_boundaries(X, y, model, ax):
h = .02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
ax.contourf(xx, yy, Z, alpha=0.8, cmap=plt.cm.coolwarm)
scatter = ax.scatter(X[:, 0], X[:, 1], c=y, edgecolor='k', marker='o', cmap=plt.cm.coolwarm)
legend1 = ax.legend(*scatter.legend_elements(), title="Classes")
ax.add_artist(legend1)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
fig, ax = plt.subplots()
plot_decision_boundaries(X_test_2D, Y_test, svm, ax)
plt.title('SVM Decision Boundary with RBF Kernel (2D Data)')
plt.legend()
plt.show()