Skip to content

Commit aa3162c

Browse files
Create 9.py
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.
1 parent 9142552 commit aa3162c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

9.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from sklearn.model_selection import train_test_split
2+
from sklearn.neighbors import KNeighborsClassifier
3+
from sklearn.metrics import classification_report, confusion_matrix
4+
from sklearn import datasets
5+
iris=datasets.load_iris()
6+
x = iris.data
7+
y = iris.target
8+
print ('sepal-length', 'sepal-width', 'petal-length', 'petal-width')
9+
print(x)
10+
print('class: 0-Iris-Setosa, 1- Iris-Versicolour, 2- Iris-Virginica')
11+
print(y)
12+
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.3)
13+
classifier = KNeighborsClassifier(n_neighbors=5)
14+
classifier.fit(x_train, y_train)
15+
y_pred=classifier.predict(x_test)
16+
print('Confusion Matrix')
17+
print(confusion_matrix(y_test,y_pred))
18+
print('Accuracy Metrics')
19+
print(classification_report(y_test,y_pred))

0 commit comments

Comments
 (0)