Error in { : task 1 failed - "could not find function "knn""

12,384

You need to call library(class) on each of the nodes. foreach makes this easy via the .packages argument.

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

You might also need to export train, test, and cl.

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

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I am trying to run a parallel kNN program on R but I get this error:

    Error in { : task 1 failed - "could not find function "knn""

    This is the program:

    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)