Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (19k 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);

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?

1 Answer

0 votes
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]]))

[0]

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

[[0.66666667 0.33333333]]

Hope this answer helps you!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...