text_plain_output_1.png
from collections import Counter

from math import sqrt

from sklearn import neighbors

from sklearn.metrics import accuracy_score

from sklearn.metrics import mean_squared_error

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

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)

rmse_values = []
for k in range(20):
    k = k + 1
    model = neighbors.KNeighborsRegressor(n_neighbors=k)
    model.fit(x_train, y_train)
    pred = model.predict(x_test)
    error = sqrt(mean_squared_error(y_test, pred))
    rmse_values.append(error)

import matplotlib.pyplot as plt

predict = model.predict(x_test)

import seaborn as sns
plt.scatter(x_test, predict)
plt.xlabel('Sepal Length')
plt.ylabel('Petal Length')