我试图使用R.的knn库中的class函数,这给我一个错误,即"train“与"class”的长度不同。
在打印火车长度和类长度时,我发现列车的长度为100 (视需要而定),而类的长度为2(如预期的那样)。如果我正确理解,cl,或类,意味着是一个因素向量的标签。我的标签只是“橙色”和“蓝色”。我遵循了文档中的示例,但是错误仍然存在。我的代码有什么明显的错误吗?任何帮助都是非常感谢的。
library(class)
x <- runif(100, 1, 100)
y <- runif(100, 1, 100)
train.df <- data.frame(x, y)
x.test <- runif(100, 1, 100)
y.test <- runif(100, 1, 100)
test.df <- data.frame(x.test, y.test)
cl <- factor(c(rep("orange", 100), rep("blue", 100)))
knn(train.df, test.df, cl, k=3, prob=TRUE)发布于 2016-10-02 06:06:31
cl有200个元素长。试着为每个类调用rep 50次。
library(class)
x <- runif(100, 1, 100)
y <- runif(100, 1, 100)
train.df <- data.frame(x, y)
x.test <- runif(100, 1, 100)
y.test <- runif(100, 1, 100)
test.df <- data.frame(x.test, y.test)
cl <- factor(c(rep("orange", 50), rep("blue", 50)))
knn(train.df, test.df, cl, k=3, prob=TRUE)https://stackoverflow.com/questions/39817064
复制相似问题