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

Bri*_*ian 11 r neural-network

使用RGUI.我有一个名为Data的数据集.我感兴趣的响应变量包含在第一列中Data.

我有训练套DataDataTrainDataTest.

随着DataTrain我训练神经网络模型(称为DataNN使用包和功能)neuralnet.

> DataNN = neuralnet(DataTrain[,1] ~ DataTrain[,2] + DataTrain[,3], hidden = 1,
    data = DataTrain) 
Run Code Online (Sandbox Code Playgroud)

有谁知道如何使用测试集(DataTest)在此模型上创建预测?

通常(对于其他型号)我会用predict()它.例如

> DataPred = predict(DataNN, DataTest)
Run Code Online (Sandbox Code Playgroud)

但是当neuralnet我这样做时,我得到:

> DataPred = predict(DataNN, DataTest)

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

显然我无法运行predict()这个模型.有谁知道任何替代品?

我检查了帮助neuralnet,我找到了一个prediction文档第12页中调用的方法.我认为这根本不是我想要的,或者至少我不知道如何将它应用到我的身上Data.

任何帮助将不胜感激(如果有任何解决方案).

小智 21

计算方法做了你是什么之后,我复制从帮助文件这个例子并增加了一些意见:

 # 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, (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.
Run Code Online (Sandbox Code Playgroud)

  • 当我使用compute时遇到了这个错误:没有适用于"计算"的方法应用于类"nn"的对象.这与dplyr中的计算方法存在冲突.我这样调用修复:neuralnet :: compute(). (6认同)