-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKNN Classification.py
More file actions
80 lines (39 loc) · 1.2 KB
/
KNN Classification.py
File metadata and controls
80 lines (39 loc) · 1.2 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
#!/usr/bin/env python
# coding: utf-8
# 11) Assignment on Classification using KNN. Build an application to classify an iris flower into its specie using KNN (Iris dataset from Skleam). Display Accuracy score, classification Report & Confusion Matrix).
# In[19]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix,classification_report,accuracy_score
from sklearn.datasets import load_iris
from sklearn import neighbors
# Loading Dataset
# In[26]:
iris=load_iris()
# Preparing X and Y
# In[27]:
X=iris.data
Y=iris.target
# Spliting X and Y
# In[21]:
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.3)
# Model Building
# In[22]:
model = neighbors.KNeighborsClassifier(n_neighbors=3)
# Model Training
# In[23]:
model.fit(X_train,Y_train)
# Model Testing
# In[24]:
y_pred=model.predict(X_test)
y_pred
# In[25]:
accuracy = accuracy_score(Y_test, y_pred)
print(f"Accuracy Score: ",accuracy)
print("\nClassification Report:")
print(classification_report(Y_test, y_pred,))
cm = confusion_matrix(Y_test, y_pred)
print("Confusion Matrix:\n", cm)