import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from collections import Counter


iris = datasets.load_iris()
X = iris.data
y = iris.target


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


def predict(X_train, y_train, x_test, k):
distances = []
for i in range(len(X_train)):
distance = np.sqrt(np.sum(np.square(x_test - X_train[i, :])))
distances.append([distance, y_train[i]])
distances = sorted(distances)
k_nearest_labels = [i[1] for i in distances[:k]]
return Counter(k_nearest_labels).most_common(1)[0][0]


y_pred = []


for x_test in X_test:
y_pred.append(predict(X_train, y_train, x_test, 5))

accuracy = np.mean(y_pred == y_test)
print("Accuracy:", accuracy)

Output of the following python program

Posted on by