Repository navigation
Expand file tree
/
Copy pathknn_task6.py
More file actions
47 lines (39 loc) · 1.43 KB
/
Copy pathknn_task6.py
File metadata and controls
47 lines (39 loc) · 1.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
# Task 6 - K-Nearest Neighbors (KNN) Classification
# Step 1: Import necessary libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay
# Step 2: Load dataset
iris = load_iris()
X = iris.data
y = iris.target
# Step 3: Split dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Step 4: Normalize features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Step 5: Try different K values
for k in range(1, 11):
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
print(f"\nFor K = {k}")
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
# Step 6: Final model with chosen K (e.g., K=3)
k = 3
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
# Step 7: Visualize Confusion Matrix
disp = ConfusionMatrixDisplay(confusion_matrix(y_test, y_pred),
display_labels=iris.target_names)
disp.plot()
plt.title(f"KNN Confusion Matrix (K={k})")
plt.show()