-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml18_ex.py
48 lines (33 loc) · 1.24 KB
/
ml18_ex.py
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
#KNN Classification
import pandas as pd
from sklearn.datasets import load_digits
digits=load_digits()
#print(digits.target) #So the targets ranges from 1 to 9
#print(dir(digits)) #show the what kind of columnsdoes this dataset will contain
#print(digits.target_names) #0 to 9
#Lets change the whole dataset into dataframe
df=pd.DataFrame(digits.data,digits.target)
#Add another column named as target
df['target']=digits.target
#Split the data
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(df.drop('target',axis='columns'),df.target,test_size=0.3,random_state=30)
#Create KNN classifier
from sklearn.neighbors import KNeighborsClassifier
knn=KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train,y_train)
print(knn.score(X_test,y_test)) #98%
#Lets create confusion matrix
from sklearn.metrics import confusion_matrix
y_pred=knn.predict(X_test)
cm=confusion_matrix(y_test,y_pred)
import matplotlib.pyplot as plt
import seaborn as sn
plt.figure(figsize=(7,5))
sn.heatmap(cm,annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')
plt.show()
#Classification Report
from sklearn.metrics import classification_report
print(classification_report(y_test,y_pred))