knn- same k, different result

血红的双手。 提交于 2021-02-19 05:55:13

问题


I have a matriz ZZ. After I ran prcomp and chose the first 5 PCs I get data_new:

P= prcomp(zz)
data_new = P$x[,1:5]

then I split into training set and test set

pca_train = data_new[1:121,]
pca_test = data_new[122:151,]

and use KNN:

k <- knn(pca_train, pca_test, tempGenre_train[,1], k = 5)
a <- data.frame(k)
res <- length(which(a!=tempGenre_test))

Each time I run these 3 last rows, I get a different value in res. Why?

Is there a better way to check what is the test error?


回答1:


From the documentation of knn,

For each row of the test set, the k nearest (in Euclidean distance) training set vectors are found, and the classification is decided by majority vote, with ties broken at random.

If you don't want the randomization to occur, you could use set.seed to ensure the same "randomization" on each run.



来源:https://stackoverflow.com/questions/31750196/knn-same-k-different-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!