human
I am working on the file "Machine Learning Basics with Sklearn.ipynb".
The first task is:
Implementing your own KNN
Let's implement a simple knn classifier with k=1.
We have to implement the fit method and the predict method.
Then initialize, train and print error.
Can you write Python code for it?
 ... Give me a Python code to print error?
assistant
#Here is Python code to print error.

from scipy.spatial import distance

class ScrappyKNN(object):
    def fit(self, X_train, y_train):
        self.X_train = X_train
        self.y_train = y_train
        return self

    def predict(self, X_test):
        predictions = []
        for row in X_test:
            label = self.closest(row)
            predictions.append(label)
        return predictions

    def closest(self, row):
        best_dist = distance.euclidean(row, self.X_train[0])
        best_index = 0
        for i in range(1, len(self.X_train)):
            dist = distance.euclidean(row, self.X_train[i])
            if dist < best_dist:
                best_dist = dist
                best_index = i

        return self.y_train[best_index]
    
clf = ScrappyKNN()
clf = clf.fit(train_X,train_y)
print(accuracy_score(test_y, clf.predict(test_X)))