简体   繁体   English

R - 与神经网络的整体?

[英]R - ensemble with neural network?

This is a small sample of my data.frame 这是我的data.frame的一个小样本

    naiveBayesPrediction knnPred5 knnPred10 dectreePrediction logressionPrediction correctClass
1                non-bob        2         2           non-bob    0.687969711847463            1
2                non-bob        2         2           non-bob     0.85851872253358            1
3                non-bob        1         1           non-bob    0.500470892627383            1
4                non-bob        1         1           non-bob     0.77762739066215            1
5                non-bob        1         2           non-bob    0.556431439357365            1
6                non-bob        1         2           non-bob    0.604868385598237            1
7                non-bob        2         2           non-bob    0.554624186182919            1

I have factored everything 我考虑了一切

   'data.frame':    505 obs. of  6 variables:
     $ naiveBayesPrediction: Factor w/ 2 levels "bob","non-bob": 2 2 2 2 2 2 2 2 2 2 ...
     $ knnPred5            : Factor w/ 2 levels "1","2": 2 2 1 1 1 1 2 1 2 1 ...
     $ knnPred10           : Factor w/ 2 levels "1","2": 2 2 1 1 2 2 2 1 2 2 ...
     $ dectreePrediction   : Factor w/ 1 level "non-bob": 1 1 1 1 1 1 1 1 1 1 ...
     $ logressionPrediction: Factor w/ 505 levels "0.205412826873861",..: 251 415 48 354 92 145 90 123 28 491 ...
     $ correctClass        : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...

I then tried to ensemble it using neuralnet 然后我尝试使用神经网络进行整合

ensembleModel <- neuralnet(correctClass ~ naiveBayesPrediction + knnPred5 + knnPred10 + dectreePrediction + logressionPrediction, data=allClassifiers[ensembleTrainSample,])

Error in neurons[[i]] %*% weights[[i]] : requires numeric/complex matrix/vector arguments 神经元[[i]]%*%权重[[i]]中的错误:需要数字/复杂矩阵/向量参数

I then tried to put in a matrix 然后我试着放入一个矩阵

m <- model.matrix( correctClass ~ naiveBayesPrediction + knnPred5 + knnPred10 + dectreePrediction + logressionPrediction, data = allClassifiers )

Error in contrasts<- ( *tmp* , value = contr.funs[1 + isOF[nn]]) : contrasts<-误差contrasts<-*tmp* ,value = contr.funs [1 + isOF [nn]]):
contrasts can be applied only to factors with 2 or more levels 对比度仅适用于具有2级或更多级别的因素

I think it must be something to do with the one feature "decistreePrediction" only having 1 level but it only finds one level out of 2 possible outcomes (bob or non-bob) so I have no idea where to go from there. 我认为它必须与一个功能“decistreePrediction”只有一个级别有关,但它只找到2个可能结果中的一个级别(bob或非bob)所以我不知道从那里去哪里。

The neuralnet function requires the 'variables' to be numeric or complex values because it is doing matrix multiplication which requires numeric or complex arguments. neuralnet函数要求'变量'是numericcomplex数值,因为它正在进行矩阵乘法,这需要numericcomplex参数。 This is very clear in the error returned: 这在返回的错误中非常清楚:

Error in neurons[[i]] %*% weights[[i]] : 
  requires numeric/complex matrix/vector arguments

This is also reflected with the following trivial example. 这也反映在以下简单的例子中。

mat <- matrix(sample(c(1,0), 9, replace=TRUE), 3)
fmat <- mat
mode(fmat) <- "character"

# no error
mat %*% mat

# error
fmat %*% fmat
Error in fmat %*% fmat : requires numeric/complex matrix/vector arguments

As a quick demonstration with the actual function I will use the infert dataset which is used as a demo within the package. 作为实际功能的快速演示,我将使用infert数据集,该数据集在包中用作演示。

library(neuralnet)
data(infert)

# error
net.infert <- neuralnet(case~as.factor(parity)+induced+spontaneous, infert)
Error in neurons[[i]] %*% weights[[i]] : 
  requires numeric/complex matrix/vector arguments

# no error
net.infert <- neuralnet(case~parity+induced+spontaneous, infert)

You can leave correctClass as a factor because it will be converted to a dummy numeric variable anyway but it may be best to also convert it to the respective binary representation. 您可以将correctClass作为一个factor因为无论如何它都将被转换为虚拟数字变量,但最好也将它转换为相应的二进制表示。

My suggestions to you are: 我给你的建议是:

  1. Convert your factors to the respective binary representations (ie 0's and 1's) 将您的因子转换为相应的二进制表示(即0和1)
  2. Leave logressionPrediction as numeric logressionPrediction保留为数字
  3. Omit variables that only have 1 value. 省略只有1个值的变量。 Including such variables is completely superfluous as no learning can be accomplished with them. 包括这些变量是完全多余的,因为不能用它们完成学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM