【问题标题】:I fail to run caret's nnet regression我无法运行插入符号的 nnet 回归
【发布时间】:2021-10-11 17:35:50
【问题描述】:

我尝试使用 caret 的 nnet 运行回归,但出现错误。

library(tidyverse)
library(caret)
feature = rnorm(100, 0, 1) %>% as.matrix() 
colnames(feature) = "x1"
outcome = rnorm(100, 0, 1) %>% as.matrix()
colnames(feature) = "y1"
model = caret::train(
  x = feature, y = outcome, method = "nnet",
  tuneGrid = expand.grid(size=c(1:3), decay=seq(0.1, 1, 0.1)),
  weights = NULL, linout = TRUE
  )

Error: Metric RMSE not applicable for classification models

当然,我想回归,而不是分类。为了显示这一点,我设置了选项linout = TRUE。出了什么问题?

另外,我关注this question,并试图删除as.matrix,但它也显示其他错误。

library(tidyverse)
library(caret)
feature = rnorm(100, 0, 1) %>% as.double()
outcome = rnorm(100, 0, 1) %>% as.double()
CATE_model = caret::train(
  x = feature, y = outcome, method = "nnet",
  tuneGrid = expand.grid(size=c(1:3), decay=seq(0.1, 1, 0.1)),
  weights = NULL, linout = TRUE
  )

Error: Please use column names for 'x'

非常感谢

【问题讨论】:

    标签: r neural-network


    【解决方案1】:

    神经网络的输入似乎需要列名。 这是有道理的,因为经过训练的神经网络稍后可能需要识别其输入中的正确列以进行预测。

    如果您将您的功能强制放入带有 colnames 的 Dataframe(在我的情况下为 x),它会起作用。

    library(tidyverse)
    library(caret)
    feature = data.frame(x = rnorm(100, 0, 1) %>% as.double()) # changed line 
    outcome = rnorm(100, 0, 1) %>% as.double()
    CATE_model = caret::train(
      x = feature, y = outcome, method = "nnet",
      tuneGrid = expand.grid(size=c(1:3), decay=seq(0.1, 1, 0.1)),
      weights = NULL, linout = TRUE
    )
    

    【讨论】:

    • 非常感谢。我第一次展示的代码尝试设置功能的名称,如下所示。 ``` feature = rnorm(100, 0, 1) %>% as.matrix() colnames(feature) = "x1" ``` 我认为 nnet 也适用于矩阵。我应该使用 data.frame 而不是矩阵吗?
    • 我想你也可以使用。我认为问题在于结果在您的第​​一段代码中被定义为矩阵。只有收入似乎需要列名。
    • 非常感谢。感谢您的建议,效果很好!
    猜你喜欢
    • 1970-01-01
    • 2019-04-25
    • 2019-11-24
    • 1970-01-01
    • 2015-08-18
    • 2017-02-06
    • 1970-01-01
    • 2021-06-29
    • 2022-12-12
    相关资源
    最近更新 更多