MATLAB Answers

simulate k nearest neighbourhood

1 view (last 30 days)
I want to simulate knn. When I add a new point on graph where other data points are on, it can predict class of it, but I don't have any idea about what to do after I upload a data set.


KSSV on 4 Jul 2019
What exactly you want to do?
Ahsen Feyza Dogan
Ahsen Feyza Dogan on 4 Jul 2019
I want to determine knn determine random point that I click on to determine which group like in this video but I tried its code does not working

Sign in to comment.

Accepted Answer

KSSV on 4 Jul 2019
Play with this:
x = rand(500,1) ; y = rand(500,1) ;
l = kmeans([x y],4) ;
hold on
N = 10 ;
for i = 1:100
% pick any point
[ptx,pty] = getpts() ;
idx = knnsearch([x y],[ptx pty],'k',N)' ;
li = mean(l(idx)) ;
plot([ptx*ones(N,1) x(idx)]',[pty*ones(N,1) y(idx)]','r')


Show 8 older comments
KSSV on 5 Jul 2019
Ahsen Feyza Dogan commented: I guess I found the reason of error. There is no correlation between x and y, they are just randomly selected numbers so classification is not correct.
KSSV on 5 Jul 2019
The result is correct in given code......code is showing up different groups/ labels. Attach your data and code to rectify the error.
KSSV on 5 Jul 2019
Why/how for loop is a trianing? For knnsearch there wont be any training. It gets the specififed number of nearest neighbors by calculating distances.
That line of plot is to show up lines joining the picked point and it's neighbors.

Sign in to comment.

More Answers (1)

Ahsen Feyza Dogan
Ahsen Feyza Dogan on 5 Jul 2019
I dont understand what does for loop do?


Sign in to comment.

Sign in to answer this question.