r - 神经网络 - 选择未定义的列

标签 r neural-network

数据分区

我有几个因子变量,因此我决定使用虚拟变量来代替 ANN。

set.seed(222)
m <- model.matrix( ~price+month+year+area+larea+otype+cid+rid+did+renovation+nrooms+nbeds+nbaths+nbalcs+wfloor+status, data = data)
ind <- sample(2, nrow(m), replace = TRUE, prob = c(0.8, 0.2))
training <- m[ind==1,]
testing <- m[ind==2,]

神经网络

n <- neuralnet(price~.,
                data = training,
                hidden = 1,
                err.fct = "sse",
                linear.output = FALSE)

Error in [.data.frame(data, , model.list$variables) : undefined columns selected

我使用点是因为我有 94​​ 个解释变量。当我对两个解释变量运行价格以查看问题所在时,它起作用了。 dot 有一些问题,它是否只用于线性回归而我弄错了?我该如何解决这个问题?

最佳答案

model.matrix 是一个matrix,而不是data.frame。根据?neuralnet

data - a data frame containing the variables specified in formula.


因此,我们可能需要使用 as.data.frame 转换为 data.frame,因为这可以与 data.frame 输入一起正常工作

library(neuralnet)
data(iris)
nn <- neuralnet(Species~ ., iris, 
        linear.output = FALSE, hidden = 1, err.fct = "sse")
nn$weights
#[[1]]
#[[1]][[1]]
#           [,1]
#[1,]  -9.900398
#[2,]  -1.527258
#[3,] -13.201669
#[4,]  11.624309
#[5,]  17.896367

#[[1]][[2]]
#          [,1]      [,2]      [,3]
#[1,]  32.51005 -4.014045 -4.303671
#[2,] -65.72300  4.013259  4.304652

在OP的代码中,问题在于model.matrix使用非标准名称创建列名称。可以使用 make.unique

将其转换为标准名称
names(training) <- make.names(names(training))
n <- neuralnet(price ~ ., 
            data = training,
            hidden = 1,
            err.fct = "sse", 
      linear.output = FALSE)



n$weights
#[[1]]
#[[1]][[1]]
#              [,1]
# [1,]  -0.62625018
# [2,]   1.39124245
# [3,]  -2.59472834
# [4,]   0.27773897
# [5,]   7.15830865
# [6,]  -2.93583230
# ...

关于r - 神经网络 - 选择未定义的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61239006/

相关文章:

r - 在 R 中使用 ROLLING 均值插补缺失值

r - 无法在 ggplot2 中显示次要网格线

R 在 renv 中安装包 stringi

java - Encog 神经网络 - 如何构建训练数据?

python - 我无法在 gridsearch 中添加优化器参数

r - 在 macOS Sierra(版本 10.12.4)和 R 版本 3.4.0 上安装 RGtk2

r - 使用 read_excel "Error: std::bad_alloc"的错误信息

machine-learning - 我认为是机器学习问题的最佳方法

python - Keras 中的深度自动编码器将一个维度转换为另一个维度

tensorflow - 如何将中间卷积层的结果存储在 tensorflow 中以供以后处理?