get.knnx中的错误(train,test,k,algorithm):ANN:ERROR -----------

时间:2019-09-23 13:15:52

标签: r knn

我第一次使用KNN,遇到了麻烦。

这是我的代码:

k_range = c(1,3,5,10,25,50,100,200,300,353)

trainMSE = c()
testMSE = c()

knnTrain <- knn.reg(train = trainCarseats[,  c("CompPrice","Income","Advertising","Price","Age")],   
                    y=trainCarseats$Sales, 
                    test = trainCarseats[,  c("CompPrice","Income","Advertising","Price","Age")], 
                    k = 5)
trainMSE <- mean((trainCarseats$Sales - knnTrain$pred)^2)

trainMSE

for(i in 1:length(k_range)){
knnTrain <- knn.reg(train = (trainCarseats[, c("CompPrice","Income","Advertising","Price","Age")]),                        y = trainCarseats$Sales, 
                    test = (trainCarseats[,c("CompPrice","Income","Advertising","Price","Age")]), 
                    k = k_range[i])
trainMSE[i] <- mean((trainCarseats$Sales - knnTrain$pred)^2)
}

for(i in 1:length(k_range)){
knnTest <- knn.reg(train = trainCarseats[, c("CompPrice","Income","Advertising","Price","Age")],   y=trainCarseats$Sales, test = testCarseats[, c("CompPrice","Income","Advertising","Price","Age")], k = k_range[i])
testMSE[i] <- mean((testCarseats$Sales - knnTest$pred)^2)
}

我一直收到此错误:

get.knnx中的错误(train,test,k,algorithm):ANN:ERROR ------->

这是怎么回事? 奇怪的是,如果我没有循环执行此操作,那么一切都很好

0 个答案:

没有答案