DanGoodrick
DanGoodrick

Reputation: 3208

What is the difference between the python scikitlearn NearestNeighbors and KNeighbors classifiers

Trying to get started with Python's SciKitLearn library but got stuck on what the difference is between the NearestNeighbors classifier and the KNeighbors classifier. It seems that the arguments are similar but not identical...

NearestNeighbors

KNeighbors

Upvotes: 1

Views: 194

Answers (3)

Thomas
Thomas

Reputation: 8968

They are really the same under the hood. See KNeighbours and Neighbours : they both have a NeighboursBase class which will do the k neighbouring

Then as @Bram mentions, Kneighbours will have some extra methods for predicting a class (which is pretty much like taking the majority of the classes from the k neighbours), it's really a question of semantics

Upvotes: 0

harshal1618
harshal1618

Reputation: 61

NearestNeighbors class does not have .predict or .predict_prob methods to predict the label of a test sample. However KNeighbors, which is a class for supervised learning has .predict and .predict_prob methods to predict label and probability of a test sample.

Upvotes: 0

Bram Zijlstra
Bram Zijlstra

Reputation: 390

NearestNeighbors is used for unsupervised learning, KNeighbors for supervised. See documentation. You use unsupervised learning for example when you want to find nearest neighbors between two datasets you use supervised learning when you want to classify based on the class of the nearest neighbors in the dataset.

Upvotes: 3

Related Questions