使用方法("compute")中出现错误:对于类别为"nn"的对象,没有适用的方法。

10

在R中使用neuralnet包的compute()函数时出现错误。

这是因为数据量太大导致的吗?我无法确定确切的问题。

 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:数据列是数字。

1个回答

18

回答

你加载了多个包,其中包含compute函数,因此你正在使用错误的函数。请强制使用neuralnet包中的compute函数:

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

推理

错误提示中提到了使用了UseMethod("compute")这一行代码。然而,在neuralnet::compute中并不存在这行代码。因此,你似乎在使用另一个包中的compute函数(比如dplyr包),而不是neuralnet包中的。可以通过使用::来避免这种情况:neuralnet::compute

附加信息

使用find函数,你可以找到定义你的函数的所有命名空间,以及R查找命名空间的顺序:

find("compute")
# [1] "package:neuralnet" "package:dplyr"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接