Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

K nearest neighbour in python [closed]

Tags:

python

scipy

knn

I would like to calculate K-nearest neighbour in python. what library should i use?

like image 462
sramij Avatar asked Apr 06 '11 11:04

sramij


People also ask

Which classifier is used in KNN in Python?

First, import the KNeighborsClassifier module and create KNN classifier object by passing argument number of neighbors in KNeighborsClassifier() function. Then, fit your model on the train set using fit() and perform prediction on the test set using predict().

How do you get the best K value in KNN Python?

The optimal K value usually found is the square root of N, where N is the total number of samples. Use an error plot or accuracy plot to find the most favorable K value. KNN performs well with multi-label classes, but you must be aware of the outliers.

What are the difficulties with K-nearest Neighbour?

Disadvantages of KNN Algorithm:Always needs to determine the value of K which may be complex some time. The computation cost is high because of calculating the distance between the data points for all the training samples.


2 Answers

scipy.spatial.cKDTree is fast and solid. For an example of using it for NN interpolation, see (ahem) inverse-distance-weighted-idw-interpolation-with-python on SO.

(If you could say e.g. "I have 1M points in 3d, and want k=5 nearest neighbors of 1k new points", you might get better answers or code examples.
What do you want to do with the neighbors once you've found them ?)

like image 65
denis Avatar answered Oct 04 '22 21:10

denis


It is natively in scipy if you're looking to do a kd-tree approach: http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.KDTree.html#scipy.spatial.KDTree

like image 31
Max Bileschi Avatar answered Oct 04 '22 22:10

Max Bileschi