在下面的代码中,我尝试将 K 最近邻与单个预测器一起使用。据我所知,示例的数量无需与 中的示例数量相train.X
匹配test.X
,但R
似乎没有正确解析我的输入。
library(ISLR)
library(class)
train=(Weekly$Year<2009)
train.X = Weekly$Lag2[train]
test.X = Weekly$Lag2[!train]
train.Direction = Weekly$Direction[train]
knn.pred = knn(train.X, test.X, train.Direction, k=1)
当上面的代码运行时,它得到了错误
Error in knn(train.X, test.X, train.Direction, k = 1) :
dims of 'test' and 'train' differ
如何修复train.X
并test.X
正确R
解析它们?