简体   繁体   中英

how to predict new cases using the neuralnet package

Using RGUI. I have a dataset called Data. The response variable that I'm interested in is contained in the first column of Data .

I have training sets of Data called DataTrain and DataTest .

With DataTrain I trained a neural network model (called DataNN ) using the package and function neuralnet .

> 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 )?

Normally (for other models) I would use predict() for this. Eg

> DataPred = predict(DataNN, DataTest)

But when doing this for neuralnet I get:

> 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. 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 . 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 .

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 .

So try DataPred = prediction(DataNN, DataTest) instead of DataPred = predict(DataNN, DataTest) .

答案是计算(nn,测试)

You should be using the neuralnet's version of 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

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

BTW never use the equals sign when assigning values to variables, unfortunately that's bad practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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