【发布时间】:2016-02-28 01:06:57
【问题描述】:
考虑以下训练集的回归问题:
我想预测每个对象的 2 最近邻预测 - 但是,每次调用 knn 函数时,我都会得到不同的预测。应该是这样吗?这是我正在使用的代码:
library(class)
test <- train <- matrix(c(-1, 0, 2, 3),,1)
cl <- c(0, 1, 2, 1)
knn(train, test, cl, k=2)
输出:
> knn(train, test, cl, k=2)
[1] 1 1 2 2
Levels: 0 1 2
> knn(train, test, cl, k=2)
[1] 0 0 1 2
Levels: 0 1 2
> knn(train, test, cl, k=2)
[1] 1 1 1 2
Levels: 0 1 2
> knn(train, test, cl, k=2)
[1] 0 0 1 2
Levels: 0 1 2
非常感谢任何澄清。
【问题讨论】: