2 views

I am working on classifying simple data using KNN with Euclidean distance. I have seen an example of what I would like to do that is done with the MATLAB knnsearch function as shown below:

x = meas(:,3:4);

gscatter(x(:,1),x(:,2),species)

newpoint = [5 1.45];

[n,d] = knnsearch(x,newpoint,'k',10);

line(x(n,1),x(n,2),'color',[.5 .5 .5],'marker','o','linestyle','none','markersize',10)

The above code takes a new point i.e. [5 1.45] and finds the 10 closest values to the new point. Can anyone please show me a MATLAB algorithm with a detailed explanation of what the search function does? Is there any other way to do this?

by (33.1k points)

You can simply use the Python Scikit-learn library to implement K nearest neighbors easily.

For example:

>>>from sklearn.neighbors import KNeighborsClassifier

>>>neigh = KNeighborsClassifier(n_neighbors=3)

>>>neigh.fit(X, y)

>>>KNeighborsClassifier()

>>>print(neigh.predict([[1.1]]))



>>>print(neigh.predict_proba([[0.9]]))

[[0.66666667 0.33333333]]