gpt4 book ai didi

r - { : task 1 failed - "could not find function "knn""中的错误

转载 作者:行者123 更新时间:2023-12-02 00:57:06 25 4
gpt4 key购买 nike

我正在尝试在 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)

最佳答案

您需要在每个节点上调用library(class)foreach 通过 .packages 参数使这变得简单。

system.time(foreach( icount(countrows), .packages="class" ) %dopar% {
summary(knn(train, test, cl, k=25, prob = TRUE))
})

您可能还需要导出 traintestcl

system.time(
foreach( icount(countrows), .packages="class",
.export=c("train","test","cl") ) %dopar% {
summary(knn(train, test, cl, k=25, prob = TRUE))
}
)

关于r - { : task 1 failed - "could not find function "knn""中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33463359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com