-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_sheet_3.py
More file actions
103 lines (65 loc) · 2.02 KB
/
lab_sheet_3.py
File metadata and controls
103 lines (65 loc) · 2.02 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
# -*- coding: utf-8 -*-
"""Lab Sheet 3.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1h-REPmmItYJLrAlYVAKivMUbYNdLmLnU
Mini Project: Iris Flower Classification using KNN
1.Import Required Libraries
"""
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.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
"""2.Load the Iris Dataset"""
data = pd.read_csv("iris.csv")
data.head()
"""3.Dataset Information"""
data.info()
"""4.Statistical Summary"""
data.describe()
"""5.Separate Features and Target"""
X = data.drop("species", axis=1)
y = data["species"]
"""6.Train–Test Split"""
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
"""7.Feature Scaling
"""
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
"""8.Build KNN Model (k = 5)"""
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train_scaled, y_train)
"""9.Make Predictions"""
y_pred = knn.predict(X_test_scaled)
y_pred
"""10.Model Accuracy"""
accuracy = accuracy_score(y_test, y_pred)
accuracy
"""11.Confusion Matrix"""
cm = confusion_matrix(y_test, y_pred)
cm
"""12.Classification Report"""
print(classification_report(y_test, y_pred))
"""13.Effect of Different k Values"""
accuracy_list = []
k_values = range(1, 21)
for k in k_values:
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train_scaled, y_train)
y_pred = knn.predict(X_test_scaled)
accuracy_list.append(accuracy_score(y_test, y_pred))
"""14.Plot Accuracy vs k"""
plt.plot(k_values, accuracy_list, marker='o')
plt.xlabel("Value of k")
plt.ylabel("Accuracy")
plt.title("KNN Accuracy for Different k Values")
plt.show()
"""15.Best k Value"""
best_k = k_values[accuracy_list.index(max(accuracy_list))]
best_k