简体   繁体   English

如何使用神经网络包预测新病例

[英]how to predict new cases using the neuralnet package

Using RGUI.使用 RGUI。 I have a dataset called Data.我有一个名为 Data 的数据集。 The response variable that I'm interested in is contained in the first column of Data .我感兴趣的响应变量包含在Data的第一列中。

I have training sets of Data called DataTrain and DataTest .我有名为DataTrainDataTestData训练集。

With DataTrain I trained a neural network model (called DataNN ) using the package and function neuralnet .通过DataTrain我使用包和函数neuralnet训练了一个神经网络模型(称为DataNN )。

> DataNN = neuralnet(DataTrain[,1] ~ DataTrain[,2] + DataTrain[,3], hidden = 1,
    data = DataTrain) 

Does anyone know how to create a prediction on this model using the test set ( DataTest )?有谁知道如何使用测试集( DataTest )对该模型进行预测?

Normally (for other models) I would use predict() for this.通常(对于其他模型)我会为此使用predict() Eg例如

> DataPred = predict(DataNN, DataTest)

But when doing this for neuralnet I get:但是当为neuralnet这样做时,我得到:

> DataPred = predict(DataNN, DataTest)

Error in UseMethod("predict") : 
no applicable method for 'predict' applied to an object of class "nn"  

Obviously I can't run predict() on this model.显然我不能在这个模型上运行predict() Does anyone know of any alternatives?有谁知道任何替代方案?

I've checked the help for neuralnet and I found a method called prediction in the page 12 of the documentation .我检查了neuralnet的帮助,我在 文档的第 12 页找到了一种称为prediction的方法。 I don't think it's what I want at all though, or at least I don't know how to apply it to my Data .我认为这根本不是我想要的,或者至少我不知道如何将它应用于我的Data

Any help would be appreciated (if there is any solution to this at all).任何帮助将不胜感激(如果有任何解决方案)。

The compute method does what you are after, I copied this example from the help file and added some comments:计算方法执行您所追求的操作,我从帮助文件中复制了此示例并添加了一些注释:

 # Make Some Training Data
 Var1 <- runif(50, 0, 100) 
 # create a vector of 50 random values, min 0, max 100, uniformly distributed
 sqrt.data <- data.frame(Var1, Sqrt=sqrt(Var1)) 
 # create a dataframe with two columns, with Var1 as the first column
 # and square root of Var1 as the second column

 # Train the neural net
 print(net.sqrt <- neuralnet(Sqrt~Var1,  sqrt.data, hidden=10, threshold=0.01))
 # train a neural net, try and predict the Sqrt values based on Var1 values
 # 10 hidden nodes

 # Compute or predict for test data, (1:10)^2
 compute(net.sqrt, as.data.frame((1:10)^2))$net.result
 # What the above is doing is using the neural net trained (net.sqrt), 
 # if we have a vector of 1^2, 2^2, 3^2 ... 10 ^2 (i.e. 1, 4, 9, 16, 25 ... 100), 
 # what would net.sqrt produce?

 Output:
 $net.result
             [,1]
 [1,] 1.110635110
 [2,] 1.979895765
 [3,] 3.013604598
 [4,] 3.987401275
 [5,] 5.004621316
 [6,] 5.999245742
 [7,] 6.989198741
 [8,] 8.007833571
 [9,] 9.016971015
[10,] 9.944642147
# The first row corresponds to the square root of 1, second row is square root
# of 2 and so on. . . So from that you can see that net.sqrt is actually 
# pretty close
# Note: Your results may vary since the values of Var1 is generated randomly.

The function for prediction is prediction , not predict .预测的功能是prediction ,而不是predict

So try DataPred = prediction(DataNN, DataTest) instead of DataPred = predict(DataNN, DataTest) .所以尝试DataPred = prediction(DataNN, DataTest)而不是DataPred = predict(DataNN, DataTest)

答案是计算(nn,测试)

You should be using the neuralnet's version of predict ie您应该使用神经网络版本的 predict ie

DataPred <- compute(DataNN, DataTest)

If you're using dplyr to do any manipulation then you'll need to specifically declare the library then the function name like so如果您使用 dplyr 进行任何操作,那么您需要专门声明库,然后是函数名称,如下所示

DataPred <- neuralnet::compute(DataNN, DataTest)

BTW never use the equals sign when assigning values to variables, unfortunately that's bad practice.顺便说一句,在为变量赋值时从不使用等号,不幸的是,这是不好的做法。

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

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