当model.matrix行计数与Neuralnet中的测试数据帧不同时进行预测

时间:2013-07-05 15:27:32

标签: r machine-learning artificial-intelligence neural-network model.matrix

我最近在使用requires numeric/complex matrix/vector arguments库时询问了有关错误neuralnet的以下问题。这是我原来的问题:“Working with neuralnet in R for the first time: get "requires numeric/complex matrix/vector arguments" but don't know how to correct”。

解决方案是使用model.matrix函数将数据框中的因子转换为“虚拟”变量。结果代码如下:

matrix.train <- model.matrix( 
  ~ survived + pclass + sex + age + sibsp + parch + fare + embarked, 
  data = train
) 

因为我的源数据框中包含许多单独的NA值,所以生成的矩阵最终会有714行,而不是原始数据帧的891行。

这对我的训练数据来说没问题。但是,当我加载我的测试数据框并将其转换为矩阵时,我遇到了同样的问题。这次我得到331个矩阵行而不是源数据帧中的418行。

在我compute之后,将模型应用于我的测试数据,我无法将cbind我的预测返回到我的测试数据,因为行数不同。所以,我的问题是:

有没有办法强制model.matrix输出与源数据框相同的行数,忽略NA个案例?我的模型需要能够处理NA并仍然输出预测,因为遇到至少有一个NA的行很常见。或者,告诉神经网络将NA值视为有效因子会更好吗?

这是我到目前为止尝试使用的代码:

#Build a matrix from training data (714 rows vs 891 rows due to NAs in data) 
matrix.train <- model.matrix(
  ~ survived + pclass + sex + age + sibsp + parch + fare + embarked, 
  data=train
)

library(neuralnet)

#Train the neural net
net <- neuralnet(
  survived ~ pclass + sexmale + age + sibsp + parch + fare + embarkedC + 
  embarkedQ + embarkedS, data=matrix.train, hidden=10, threshold=0.01
)

#Build a matrix from test data (331 rows vs 418 rows due to NAs in data)
matrix.test <- model.matrix(~ pclass + sex + age + sibsp + parch + fare + embarked, 
  data=test
)

#Apply neural net to test matrix 
net.results <- compute(
  net, matrix.test
)

#Attempt to map results back to original test data
cleanoutput <- cbind(
  net.results$net.result,test
)

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 331, 418

当我尝试使用列车数据框中的rownames强制将matrix.model矩阵强制为相同的行数时,我得到以下内容:

matrix.train <- matrix.train[match(rownames(train),rownames(matrix.train)),]

> matrix.train
    (Intercept) survived pclass sexmale   age sibsp parch     fare embarkedC embarkedQ embarkedS
1             1        0      3       1 22.00     1     0   7.2500         0         0         1
2             1        1      1       0 38.00     1     0  71.2833         1         0         0
3             1        1      3       0 26.00     0     0   7.9250         0         0         1
4             1        1      1       0 35.00     1     0  53.1000         0         0         1
5             1        0      3       1 35.00     0     0   8.0500         0         0         1
6            NA       NA     NA      NA    NA    NA    NA       NA        NA        NA        NA
7             1        0      1       1 54.00     0     0  51.8625         0         0         1

但是,那一排NAs是不准确的。实际上,该行中可能只有一个NA值,但由于某种原因,当行中列出一个NA值时,矩阵将整行变为NA。而不是上述内容,我希望看到这一点:

> matrix.train
    (Intercept) survived pclass sexmale   age sibsp parch     fare embarkedC embarkedQ embarkedS
1             1        0      3       1 22.00     1     0   7.2500         0         0           1
2             1        1      1       0 38.00     1     0  71.2833         1         0         0
3             1        1      3       0 26.00     0     0   7.9250         0         0         1
4             1        1      1       0 35.00     1     0  53.1000         0         0         1
5             1        0      3       1 35.00     0     0   8.0500         0         0         1
6             1        0      3       1 NA        0     0   6.25           1         0        NA
7             1        0      1       1 54.00     0     0  51.8625         0         0         1

0 个答案:

没有答案