-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab4.py
More file actions
326 lines (257 loc) · 10.7 KB
/
Copy pathlab4.py
File metadata and controls
326 lines (257 loc) · 10.7 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# A1 - Evaluate Confusion Matrix and Classification Report (separate file setup)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.impute import SimpleImputer
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, classification_report
# Load data
df = pd.read_csv('iotsim-air-quality-1.csv')
# Label encoding
if df['label'].dtype == 'object':
le = LabelEncoder()
df['label'] = le.fit_transform(df['label'])
print("Label mapping:", dict(zip(le.classes_, le.transform(le.classes_))))
# Filter top 2 classes
top_two = df['label'].value_counts().index[:2].tolist()
filtered_df = df[df['label'].isin(top_two)].copy()
# Features and labels
y = filtered_df['label']
X = filtered_df.drop(columns=['label'])
# Select numeric features only
X = X.select_dtypes(include=['int64', 'float64'])
# Handle missing values
imputer = SimpleImputer(strategy='mean')
X_imputed = imputer.fit_transform(X)
# Split data
X_train, X_test, y_train, y_test = train_test_split(X_imputed, y, test_size=0.3, random_state=42)
# Train KNN model
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
# Predictions
y_train_pred = knn.predict(X_train)
y_test_pred = knn.predict(X_test)
# Evaluation: classification_report, confusion_matrix is an inbuilt function in sklearn.metrics
print("\n--- A1: Confusion Matrix & Performance Metrics ---")
print("\nTrain Confusion Matrix:")
print(confusion_matrix(y_train, y_train_pred))
print("\nTest Confusion Matrix:")
print(confusion_matrix(y_test, y_test_pred))
print("\nTrain Classification Report:")
print(classification_report(y_train, y_train_pred))
print("\nTest Classification Report:")
print(classification_report(y_test, y_test_pred))
#Q2
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error, mean_absolute_percentage_error, r2_score
def load_data(filepath):
df = pd.read_excel(filepath, sheet_name="Purchase data", usecols='A:E')
return df
def matrices_AC(df):
A = df.iloc[:, 1:-1].values
C = df.iloc[:, -1].values.reshape(-1, 1)
return A, C
def compute_prices(A, C):
pseudo_inv = np.linalg.pinv(A)
return pseudo_inv @ C
def predict_prices(A, price_vector):
return A @ price_vector
def compute_metrics(actual, predicted):
mse = mean_squared_error(actual, predicted)
rmse = np.sqrt(mse)
mape = mean_absolute_percentage_error(actual, predicted)
r2 = r2_score(actual, predicted)
return mse, rmse, mape, r2
def analyze_results(mse, rmse, mape, r2):
print(f"MSE: {mse:.4f}")
print(f"RMSE: {rmse:.4f}")
print(f"MAPE: {mape:.4f}")
print(f"R²: {r2:.4f}")
if __name__ == "__main__":
file_path = "Lab Session Data.xlsx"
df = load_data(file_path)
A, C = matrices_AC(df)
price_vector = compute_prices(A, C)
predicted_C = predict_prices(A, price_vector)
mse, rmse, mape, r2 = compute_metrics(C, predicted_C)
analyze_results(mse, rmse, mape, r2)
#Q3
import numpy as np
import matplotlib.pyplot as plt
# Step 1: 20 random (X, Y) points between 1 and 10
np.random.seed(42) # for reproducibility
X = np.random.uniform(1, 10, size=(20, 2))
# Step 2: Assigned class labels based on simple rule
# if X + Y > 12, it's class 1, else class 0
threshold = 12
labels = np.where(X[:, 0] + X[:, 1] > threshold, 1, 0)
# Step 3: Plot
colors = ['blue' if label == 0 else 'red' for label in labels]
plt.figure(figsize=(8, 6))
plt.scatter(X[:, 0], X[:, 1], c=colors, s=80, edgecolors='k')
plt.title("Scatter Plot of 2D Data Classified into Two Classes")
plt.xlabel("Feature X")
plt.ylabel("Feature Y")
plt.grid(True)
plt.show()
#No Overlap Between Classes. Based on the rule, there’s no ambiguity — it's a clean separation. This helps in training initial classifiers like kNN without noise or mislabels.
#Q4
# A4 - Test Set Classification with kNN (k=3)
from sklearn.neighbors import KNeighborsClassifier
# Step 1: Generate training data again (same from A3)
np.random.seed(42)
X_train = np.random.uniform(1, 10, size=(20, 2))
y_train = np.where(X_train[:, 0] + X_train[:, 1] > 12, 1, 0)
# Step 2: Generate test grid (0 to 10, step 0.1 → 100x100 = 10,000 points)
x_vals = np.arange(0, 10.1, 0.1)
y_vals = np.arange(0, 10.1, 0.1)
xx, yy = np.meshgrid(x_vals, y_vals)
test_points = np.c_[xx.ravel(), yy.ravel()] #Now shape becomes (10000,2)
# Step 3: Fit kNN and predict
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
y_test_pred = knn.predict(test_points)
# Step 4: Plot the classification result
colors = ['blue' if label == 0 else 'red' for label in y_test_pred]
plt.figure(figsize=(10, 8))
plt.scatter(test_points[:, 0], test_points[:, 1], c=colors, s=10, alpha=0.6, marker='s')
plt.scatter(X_train[:, 0], X_train[:, 1], c=['blue' if l==0 else 'red' for l in y_train],
edgecolors='k', s=100, label='Train Points')
plt.title("A4: Test Set Classified using kNN (k=3)")
plt.xlabel("Feature X")
plt.ylabel("Feature Y")
plt.legend()
plt.grid(True)
plt.show()
#A5
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
# A3 - Generate random training data
np.random.seed(0)
X = np.random.randint(1, 11, size=(20, 2))
Y = np.array([0 if x[0] + x[1] < 12 else 1 for x in X]) # Class 0: blue, Class 1: red
# A4 - Create 100x100 grid of test points
x_min, x_max = 0, 11
y_min, y_max = 0, 11
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100))
test_points = np.c_[xx.ravel(), yy.ravel()] # Shape: (10000, 2)
# A5 - k = 1, 5, 7 and plot results
ks = [1, 5, 7]
plt.figure(figsize=(18, 5))
for idx, k in enumerate(ks):
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X, Y)
Z = knn.predict(test_points)
Z = Z.reshape(xx.shape)
plt.subplot(1, 3, idx + 1)
plt.contourf(xx, yy, Z, cmap=plt.cm.RdBu, alpha=0.5)
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.RdBu, edgecolors='k')
plt.title(f"k = {k}")
plt.xlabel("X1")
plt.ylabel("X2")
plt.tight_layout()
plt.show()
#Smaller k → overfitting, more complex boundaries.Larger k → underfitting, smoother boundaries.
#A6
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
# === Step 1: Load and Filter the Dataset ===
df = pd.read_csv('iotsim-air-quality-1.csv')
# Keep only required classes
df = df[df['label'].isin(['Telnet Brute Force', 'TCP Scan'])]
# Drop rows with missing values in selected columns
df = df[['ip.ttl', 'tcp.window_size_value', 'label']].dropna() #label: Target
# Features and target
features = ['ip.ttl', 'tcp.window_size_value']
X = df[features].values
le = LabelEncoder()
y = le.fit_transform(df['label']) # Encodes: 'TCP Scan' = 1, 'Telnet Brute Force' = 0
# === A3: Scatter plot with true labels ===
plt.figure(figsize=(8, 6))
colors = ['blue' if label == 0 else 'red' for label in y]
plt.scatter(X[:, 0], X[:, 1], c=colors, edgecolors='k', s=80)
plt.title("A3: Real Data - 2D Scatter Plot")
plt.xlabel("ip.ttl")
plt.ylabel("tcp.window_size_value")
plt.grid(True)
plt.show()
#The selected features ip.ttl and tcp.window_size_value allow some degree of class separation between Telnet Brute Force and TCP Scan.The overlap exists at (64, 0)
# === A4: Decision boundary with k = 3 ===
x_min, x_max = X[:, 0].min() - 5, X[:, 0].max() + 5
y_min, y_max = X[:, 1].min() - 5000, X[:, 1].max() + 5000
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 200),
np.linspace(y_min, y_max, 200))
test_points = np.c_[xx.ravel(), yy.ravel()]
clf = KNeighborsClassifier(n_neighbors=3)
clf.fit(X, y)
Z = clf.predict(test_points).reshape(xx.shape)
plt.figure(figsize=(10, 8))
plt.contourf(xx, yy, Z, cmap=plt.cm.RdBu, alpha=0.4)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.RdBu, edgecolors='k')
plt.title("A4: k-NN Classification (k=3)")
plt.xlabel("ip.ttl")
plt.ylabel("tcp.window_size_value")
plt.grid(True)
plt.show()
#The decision boundary is not a perfect straight line — it curves around clusters.Some blue and red points are mixed in overlapping regions, especially near low values of tcp.window_size_value (e.g. around (64, 0)).This shows that like in A4, k-NN handles non-linear class separation and adapts to local data structure.
# === A5: Repeat for k = 1, 5, 7 ===
ks = [1, 5, 7]
plt.figure(figsize=(18, 5))
for i, k in enumerate(ks):
clf = KNeighborsClassifier(n_neighbors=k)
clf.fit(X, y)
Z = clf.predict(test_points).reshape(xx.shape)
plt.subplot(1, 3, i + 1)
plt.contourf(xx, yy, Z, cmap=plt.cm.RdBu, alpha=0.4)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.RdBu, edgecolors='k')
plt.title(f"A5: k = {k}")
plt.xlabel("ip.ttl")
plt.ylabel("tcp.window_size_value")
plt.tight_layout()
plt.show()
#smaller k = overfit, larger k = smooth boundary. So for our project data, moderate k (like 5 or 7) performs better, reducing misclassification in overlapping areas.
#A7- Grid Search for Optimal k
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
# === Step 1: Train-test split ===
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# === Step 2: Grid Search over k values ===
param_grid = {'n_neighbors': list(range(1, 21))} # Try k = 1 to 20
grid_search = GridSearchCV(KNeighborsClassifier(), param_grid, cv=5)
grid_search.fit(X_train, y_train)
# Best k found
best_k = grid_search.best_params_['n_neighbors']
print("A7: Best k value found:", best_k)
# === Step 3: Fit final model and evaluate ===
best_clf = KNeighborsClassifier(n_neighbors=best_k)
best_clf.fit(X_train, y_train)
y_pred = best_clf.predict(X_test)
# === Step 4: Accuracy ===
acc = accuracy_score(y_test, y_pred)
print("A7: Accuracy with best k =", acc)
from sklearn.model_selection import RandomizedSearchCV, train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import numpy as np
# === Step 1: Train-test split ===
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# === Step 2: Random Search over k values ===
param_dist = {'n_neighbors': list(range(1, 30))} # Try k = 1 to 20
random_search = RandomizedSearchCV(KNeighborsClassifier(), param_distributions=param_dist, n_iter=10, cv=5, random_state=42)
random_search.fit(X_train, y_train)
# Best k found
best_k = random_search.best_params_['n_neighbors']
print("A7 (RandomSearchCV): Best k value found:", best_k)
# === Step 3: Fit final model and evaluate ===
best_clf = KNeighborsClassifier(n_neighbors=best_k)
best_clf.fit(X_train, y_train)
y_pred = best_clf.predict(X_test)
# === Step 4: Accuracy ===
acc = accuracy_score(y_test, y_pred)
print("A7 (RandomSearchCV): Accuracy with best k =", acc)