1

Why my KNN Classifier build from scratch with numpy gives different results than the sklearn.KNeighborsClassifier? What is wrong with my code?

# create a function that computes euclidean distance and return the most common class label 
    # for given k.
    def k_neighbors(self, x):
        lengths = [self.euclidean_length(x, x_train) for x_train in self.X_training] 
        k_index = np.argsort(lengths)[: self.k] 
        k_nearest_labels = [self.y_training[i] for i in k_index] 
        counts = np.bincount(k_nearest_labels) 
        most_common_label = np.argmax(counts) 
        return most_common_label
# running KNN classifier with K=5 to fit the data and make predictions.
classifier1 = KNN_Classifier(k=5) 
classifier1.fit(X_training, y_training)
predicted1 = classifier1.predicting(X_test)

They both apparently do the same but I have different outcomes. Where is the bug in my code?

from sklearn.neighbors import KNeighborsClassifier

classifier2 = KNeighborsClassifier(n_neighbors=5, algorithm='brute', p=2)
classifier2.fit(X_training, y_training)
predicted2 = classifier2.predict(X_test)

1 Answer 1

1

Based on sklearn documentation, there are multiple reasons:

  1. Distance metric: you are using Euclidean distance metric, while sklearn by default uses minkowski which in X,Y make differences
  2. To find k nearest neighbours, sklearn, by default, choose one of the kd_tree, BallTree and BruteForce methods, however, in your k_neighbours() function, you use BruteForce.
  3. Last but not least, k value in your test is 5, while you're using 4 for skleran equivalent
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man, that was helpful! Kind regards.
I changed the sklearn.KNeighborsClassifier parameters to match with what you said but still not the same results. You can see in the code above. Any other suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.