这是我的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
我已经考虑了一切
'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 ...
然后我尝试使用Neuronet进行合奏
ensembleModel <- neuralnet(correctClass ~ naiveBayesPrediction + knnPred5 + knnPred10 + dectreePrediction + logressionPrediction, data=allClassifiers[ensembleTrainSample,])
然后我尝试放入一个矩阵
m <- model.matrix( correctClass ~ naiveBayesPrediction + knnPred5 + knnPred10 + dectreePrediction + logressionPrediction, data = allClassifiers )
我认为这与“decistreePrediction”一个功能只有一个级别有关,但它只能从2种可能的结果(bob或non-bob)中找到一个级别,因此我不知道从那里去哪里。
最佳答案
neuralnet
函数要求“变量”为numeric
或complex
值,因为它正在做矩阵乘法,需要numeric
或complex
参数。这在返回的错误中非常清楚:
Error in neurons[[i]] %*% weights[[i]] :
requires numeric/complex matrix/vector arguments
下面的简单示例也反射(reflect)了这一点。
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
作为实际功能的快速演示,我将使用
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)
您可以将
correctClass
保留为factor
,因为无论如何它将被转换为虚拟数字变量,但是最好也将其转换为相应的二进制表示形式。我对您的建议是:
logressionPrediction
保留为数字关于R-与神经网络集成吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29798851/