我正在尝试在R上运行并行kNN程序,但是我收到了这个错误:
{:任务1失败 - “无法找到功能”knn“”
出错这是该计划:
library(class)
library(doSNOW)
library(foreach)
train <- read.csv('train.csv')
test <- read.csv('test.csv')
trainY <- read.csv('trainY.csv')
cl <- as.vector(as.matrix(trainY))
system.time(summary(knn(train, test, cl, k=25, prob = TRUE)))
clus <- makeCluster(4)
registerDoSNOW(clus)
countrows=nrow(test)
system.time(foreach( icount(countrows) ) %dopar% {
summary(knn(train, test, cl, k=25, prob = TRUE))
})
stopCluster(clus)
答案 0 :(得分:5)
您需要在每个节点上调用library(class)
。 foreach
通过.packages
参数轻松实现这一点。
system.time(foreach( icount(countrows), .packages="class" ) %dopar% {
summary(knn(train, test, cl, k=25, prob = TRUE))
})
您可能还需要导出train
,test
和cl
。
system.time(
foreach( icount(countrows), .packages="class",
.export=c("train","test","cl") ) %dopar% {
summary(knn(train, test, cl, k=25, prob = TRUE))
}
)