0 votes
1 view
in Machine Learning by (15.7k points)

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:

load fisheriris 

x = meas(:,3:4);


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?

1 Answer

0 votes
by (33.2k 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) 





[[0.66666667 0.33333333]]

Hope this answer helps you!

Welcome to Intellipaat Community. Get your technical queries answered by top developers !