import numpy as np import pandas as pd from sklearn import datasets iris = datasets.load_iris() labels = pd.DataFrame(iris.target) data = pd.DataFrame(iris.data) from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import confusion_matrix, accuracy_score #k = 3 classifier = KNeighborsClassifier(n_neighbors=3) classifier.fit(data, labels) y_pred = classifier.predict(data) #confusion matrix cm = confusion_matrix(labels, y_pred) print(cm) #accuracy accuracy = accuracy_score(labels, y_pred) * 100 print(accuracy)