Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in UseMethod("compute"): no applicable method for 'compute' applied to an object of class "nn"

Tags:

r

Getting the error while running compute() from the neuralnet package in R.

Is it happening because of Data Size? I can't figure out the exact problem.

 df2 <- read.csv("data.csv")
 train_df <- df2[1:3200,]
 test_df <- df2[3201:4004,]

 n <- names(train_df)
 f <- as.formula(paste("tenure ~", paste(n[!n %in% "tenure"], collapse = 
                  "+")))

 model2 <- neuralnet(f,train_df, hidden=3, threshold=0.01, linear.output=T)

 summary(model2)

 #Output
                   Length  Class      Mode    
 call                      6 -none-     call    
 response               3200 -none-     numeric 
 covariate           4118400 -none-     numeric 
 model.list                2 -none-     list    
 err.fct                   1 -none-     function
 act.fct                   1 -none-     function
 linear.output             1 -none-     logical 
 data                   1288 data.frame list    
 net.result                1 -none-     list    
 weights                   1 -none-     list    
 startweights              1 -none-     list    
 generalized.weights       1 -none-     list    
  result.matrix          3871 -none-     numeric 


 results <- compute(model2, test_df)

 #Error
 Error in UseMethod("compute"): no applicable method for 'compute' applied 
 to an object of class "nn"
 Traceback:

 1. compute(model2, test_df)

P.S: Data Columns are numeric.

like image 390
Sociopath Avatar asked Sep 21 '17 20:09

Sociopath


1 Answers

Answer

You have multiple packages loaded that contain the compute function, and consequently you are using the wrong one. Force using compute from the neuralnet package:

results <- neuralnet::compute(model2, test_df)

Reasoning

The error says it used the line UseMethod("compute"). This line of code is not present in neuralnet::compute. Therefore, you seem to be using compute from a different package. This can happen when you load the neuralnet package followed by another package that contains a compute function (like the dplyr package). You can avoid this by using ::: neuralnet::compute.

Additional information

With find, you can find all the namespaces in which your function is defined, as well as the order in which R will look through the namespaces:

find("compute")
# [1] "package:neuralnet" "package:dplyr"
like image 132
slamballais Avatar answered Sep 21 '22 13:09

slamballais