There's another way to find "nearest neighbor's type".
It's a way of using internal built-in function calls "mean".
You first sort the list. Suppose you store the sorted result in
"sortresult". Then for "K"-NN, you can use the following code:
km <- mean( sortresult[1:K] )
if( km > 1.5 ) type <- 2
else type <- 1
This avoids using any 'for' loops which is really slow in R.
Why this work? If there are more "1" than "2", the mean should be
less than 1.5, other wise it should be greater than 1.5 .
※ 引述《kinda (天天)》之铭言:
: I count the time of "Matrix multiply" and "Find nearest neighbor's type".
: The result
: [1] "matrix multiple cost time: 6.65000000000023"
: [1] "get min cost time: 67.229999999987"
: [1] "k= 1 total instance= 16000"
: [1] "correct rate= 0.92125"
: [1] "total cost time= 77.36"
: I found that "the time of find nearest's type"(67 sec) is much larger than
: "the time of Matrix multiply"(6.6 sec).
: I guess this is the difference of 100hr and 50hr.
: Following is the related part of my code.
: XX/YY is the vector of "inner product of each training/valid set vector"
: le,va is the training and valid set array.
: