用R中的神经网络计算?

compute with neural network in R?

allClassifiers 元组中的所有元组都是 1 或 2 例如

    naiveBayesPrediction    knnPred5    knnPred10   dectreePrediction   logressionPrediction    correctClass
    1                       2                1           1                     1                          1
    1                       2                1           1                     1                          1
    1                       2                1           1                     1                          1
    1                       2                1           2                     1                          1

我训练了集成器

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

但我正在尝试用它来预测:

compute(ensembleModel, allClassifiers[ensembleTestSample,])$net.result

但我收到此错误:

Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

我的训练和测试样本

ensembleTrainSample <- sample(nrow(allClassifiers), nrow(allClassifiers)*0.7)
ensembleTestSample <- (1:nrow(allClassifiers))[!(1:nrow(allClassifiers))%in% ensembleTrainSample]

与您的其他问题类似,这是一个源于矩阵乘法的错误。本质上是以下错误:

Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

表示您的矩阵没有适合矩阵乘法的维数。这就像尝试将 4x4 矩阵乘以 10x10 矩阵。它根本行不通。

您收到此错误的原因是您忽略了文档中的某些内容。如果您查看 ?compute,您将看到以下关于 covariate 参数的注释`:

covariate   a dataframe or matrix containing the variables 
            that had been used to train the neural network.

这里的关键是 VARIABLES,而不是你的整个数据集或分类器变量(你正试图预测这个!)。这里又是一个使用 infert 数据集的示例。

library(neuralnet)
data(infert)

# fit your neuralnet model
net.infert <- neuralnet(case~parity+induced+spontaneous, infert)

net.pred <- compute(net.infert, infert[,c("case","parity","induced","spontaneous")])

Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

但是如果我只包含我用来创建模型的变量,它就可以正常工作。

# no error
net.pred <- compute(net.infert, infert[,c("parity","induced","spontaneous")])