text_plain_output_1.png
from collections import Counter

from sklearn.metrics import accuracy_score

import numpy as np

import numpy as np # linear algebra

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

def train(x_train, y_train):
    return
def kNearestNeighbor(x_train, y_train, x_test, predictions, k):
    train(x_train, y_train)
    for i in range(len(x_test)):
        predictions.append(predict(x_train, y_train, x_test.values[i, :], k))

predictions = []
from sklearn.metrics import accuracy_score
kNearestNeighbor(x_train, y_train, x_test, predictions, 9)
predictions = np.asarray(predictions)
accuracy = accuracy_score(y_test, predictions)
for i in range(len(x_test)):
    print('Flower with sepal length', x_test.iloc[i], ':')
    print('belongs to the kingdom', predictions[i])