9. AI AND MACHINE LEARNING VTU LAB | READ NOW
MACHINE LEARNING VTU LAB k-Nearest Neighbour Algorithm
Program 9. WRITE A PROGRAM TO IMPLEMENT K-NEAREST NEIGHBOUR ALGORITHM TO CLASSIFY THE IRIS DATA SET. PRINT BOTH CORRECT AND WRONG PREDICTIONS. JAVA/PYTHON ML LIBRARY CLASSES CAN BE USED FOR THIS PROBLEM.
Program Code – lab9.py
from sklearn.datasets import load_iris from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import train_test_split import numpy as np dataset=load_iris() #print(dataset) X_train,X_test,y_train,y_test=train_test_split(dataset["data"],dataset["target"],random_state=0) kn=KNeighborsClassifier(n_neighbors=1) kn.fit(X_train,y_train) for i in range(len(X_test)): x=X_test[i] x_new=np.array([x]) prediction=kn.predict(x_new) print("TARGET=",y_test[i],dataset["target_names"][y_test[i]],"PREDICTED=",prediction,dataset["target_names"][prediction]) print(kn.score(X_test,y_test))
MACHINE LEARNING Program Execution – lab9.ipynb
Jupyter Notebook program execution.
from sklearn.datasets import load_iris from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import train_test_split import numpy as np
dataset=load_iris() #print(dataset) X_train,X_test,y_train,y_test=train_test_split(dataset["data"],dataset["target"],random_state=0)
kn=KNeighborsClassifier(n_neighbors=1) kn.fit(X_train,y_train)
KNeighborsClassifier(algorithm=’auto’, leaf_size=30, metric=’minkowski’, metric_params=None, n_jobs=None, n_neighbors=1, p=2, weights=’uniform’)
for i in range(len(X_test)): x=X_test[i] x_new=np.array([x]) prediction=kn.predict(x_new) print("TARGET=",y_test[i],dataset["target_names"][y_test[i]],"PREDICTED=",prediction,dataset["target_names"][prediction]) print(kn.score(X_test,y_test))
TARGET= 2 virginica PREDICTED= [2] ['virginica'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 0 setosa PREDICTED= [0] ['setosa'] TARGET= 2 virginica PREDICTED= [2] ['virginica'] TARGET= 0 setosa PREDICTED= [0] ['setosa'] TARGET= 2 virginica PREDICTED= [2] ['virginica'] TARGET= 0 setosa PREDICTED= [0] ['setosa'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 2 virginica PREDICTED= [2] ['virginica'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 0 setosa PREDICTED= [0] ['setosa'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 0 setosa PREDICTED= [0] ['setosa'] TARGET= 0 setosa PREDICTED= [0] ['setosa'] TARGET= 2 virginica PREDICTED= [2] ['virginica'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 0 setosa PREDICTED= [0] ['setosa'] TARGET= 0 setosa PREDICTED= [0] ['setosa'] TARGET= 2 virginica PREDICTED= [2] ['virginica'] show more (open the raw output data in a text editor) ... TARGET= 2 virginica PREDICTED= [2] ['virginica'] TARGET= 1 versicolor PREDICTED= [1] ['versicolor'] TARGET= 0 setosa PREDICTED= [0] ['setosa'] TARGET= 1 versicolor PREDICTED= [2] ['virginica'] 0.9736842105263158
Alternative – alt lab9.ipynb
from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import classification_report from sklearn.metrics import confusion_matrix
import pandas as pd import numpy as np from sklearn import datasets
iris=datasets.load_iris() iris_data=iris.data iris_labels=iris.target print(iris_data)
[[5.1 3.5 1.4 0.2] [4.9 3. 1.4 0.2] [4.7 3.2 1.3 0.2] [4.6 3.1 1.5 0.2] [5. 3.6 1.4 0.2] [5.4 3.9 1.7 0.4] [4.6 3.4 1.4 0.3] [5. 3.4 1.5 0.2] [4.4 2.9 1.4 0.2] [4.9 3.1 1.5 0.1] [5.4 3.7 1.5 0.2] [4.8 3.4 1.6 0.2] [4.8 3. 1.4 0.1] [4.3 3. 1.1 0.1] [5.8 4. 1.2 0.2] [5.7 4.4 1.5 0.4] [5.4 3.9 1.3 0.4] [5.1 3.5 1.4 0.3] [5.7 3.8 1.7 0.3] [5.1 3.8 1.5 0.3] [5.4 3.4 1.7 0.2] [5.1 3.7 1.5 0.4] [4.6 3.6 1. 0.2] [5.1 3.3 1.7 0.5] [4.8 3.4 1.9 0.2] show more (open the raw output data in a text editor) ... [6.7 3. 5.2 2.3] [6.3 2.5 5. 1.9] [6.5 3. 5.2 2. ] [6.2 3.4 5.4 2.3] [5.9 3. 5.1 1.8]]
x_train, x_test, y_train, y_test=(train_test_split(iris_data, iris_labels, test_size=0.20)) classifier=KNeighborsClassifier(n_neighbors=6) classifier.fit(x_train, y_train) y_pred=classifier.predict(x_test)
print("accuracy is") print(classification_report(y_test, y_pred))
accuracy is precision recall f1-score support 0 1.00 1.00 1.00 9 1 1.00 0.93 0.96 14 2 0.88 1.00 0.93 7 accuracy 0.97 30 macro avg 0.96 0.98 0.97 30 weighted avg 0.97 0.97 0.97 30