【问题标题】:Error in UseMethod("compute"): no applicable method for 'compute' applied to an object of class "nn"UseMethod(“compute”)中的错误:没有适用于“compute”的方法应用于“nn”类的对象
【发布时间】:2018-03-03 07:14:51
【问题描述】:

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

【问题讨论】:

    标签: r


    【解决方案1】:

    回答

    您加载了多个包含compute 函数的包,因此您使用了错误的包。强制使用neuralnet 包中的compute

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

    推理

    错误说它使用了UseMethod("compute")这一行。这行代码在neuralnet::compute 中不存在。因此,您似乎正在使用来自不同包的compute。当您加载neuralnet 包后跟另一个包含compute 函数的包(如dplyr 包)时,可能会发生这种情况。你可以通过使用:::neuralnet::compute来避免这种情况。

    其他信息

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 2022-12-15
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多