-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML4
More file actions
281 lines (208 loc) · 8.43 KB
/
ML4
File metadata and controls
281 lines (208 loc) · 8.43 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
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
import matplotlib.pyplot as plt
df=pd.read_csv(r'C:\Users\user\Desktop\26_ML\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=5)
#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)
plt.show()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, precision_score
# Load the dataset
df = pd.read_csv('emails.csv') # Adjust the path if needed
# Print column names to identify the correct target column
print("Columns in the dataset:", df.columns)
# Use the correct column name for labels
target_column = 'Prediction' # Update this to the actual column name in your dataset
# Check if the target column exists
if target_column not in df.columns:
raise ValueError(f"Target column '{target_column}' not found in the DataFrame. Available columns are: {df.columns}")
# Convert labels to binary values
df[target_column] = df[target_column].map({'spam': 1, 'ham': 0})
# Drop rows with missing target values
df = df.dropna(subset=[target_column])
# Separate features and target variable
X = df.drop(target_column, axis=1)
y = df[target_column]
# Check for and handle missing values in features
if X.isnull().sum().sum() > 0:
print("Missing values found in features. Filling with mean values.")
X = X.fillna(X.mean())
# Print shape of X and y before converting categorical variables
print(f"Shape of X before conversion: {X.shape}")
# Convert categorical features to numeric
X = pd.get_dummies(X)
# Print shape of X after converting categorical variables
print(f"Shape of X after conversion: {X.shape}")
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Check the shapes of the train and test sets
print(f"Shape of X_train: {X_train.shape}")
print(f"Shape of X_test: {X_test.shape}")
print(f"Shape of y_train: {y_train.shape}")
print(f"Shape of y_test: {y_test.shape}")
# Scale features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Initialize and train the KNN classifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train_scaled, y_train)
# Make predictions
y_pred = knn.predict(X_test_scaled)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
# Print the results
print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Problem Statement-Apply K-Nearest Neighbor Classifier on
Data set. Test for Accuracy and Precision.
Classify the email using the binary
classification method. Email Spam detection has two states: a) Normal State –
Not Spam, b) Abnormal State – Spam. Use K-Nearest Neighbors for classification.
Code:
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
import matplotlib.pyplot as plt
df=pd.read_csv(r'C:\Users\user\Desktop\26_ML\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=5)
#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)
plt.show()
/////////////////////////////////////////////////////////////////////////////////
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, precision_score
# Load the dataset
df = pd.read_csv('emails.csv') # Adjust the path if needed
print("Initial DataFrame:")
print(df.head()) # Print the first few rows
# Print the unique values in the target column
target_column = 'Prediction' # Update this to the actual column name in your dataset
print("Unique values in target column before mapping:", df[target_column].unique())
# Check for missing values in the target column
print("Missing values in target column:", df[target_column].isnull().sum())
# Commented out the mapping since values are already 0 and 1
# df[target_column] = df[target_column].map({'spam': 1, 'ham': 0})
# Drop rows with missing target values - Not needed here as we confirmed no missing values
# df = df.dropna(subset=[target_column])
# Check if the DataFrame is empty after dropping missing values
if df.shape[0] == 0:
raise ValueError("No samples left after preprocessing. Check your dataset and processing steps.")
# Separate features and target variable
X = df.drop(target_column, axis=1)
y = df[target_column]
# Check for and handle missing values in features
if X.isnull().sum().sum() > 0:
print("Missing values found in features. Filling with mean values.")
X = X.fillna(X.mean())
# Print shape of X and y before converting categorical variables
print(f"Shape of X before conversion: {X.shape}")
# Convert categorical features to numeric
X = pd.get_dummies(X)
# Print shape of X after converting categorical variables
print(f"Shape of X after conversion: {X.shape}")
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Check the shapes of the train and test sets
print(f"Shape of X_train: {X_train.shape}")
print(f"Shape of X_test: {X_test.shape}")
print(f"Shape of y_train: {y_train.shape}")
print(f"Shape of y_test: {y_test.shape}")
# Scale features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Initialize and train the KNN classifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train_scaled, y_train)
# Make predictions
y_pred = knn.predict(X_test_scaled)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
# Print the results
print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")