rmachine-learningneural-networkprediction

Converting neural net probablities into predictions in R


Excuse me for my ignorance, as I am learning the theory behind models and in this case neural nets. I trying to train a model using the library(nnet) library(NeuralNetTools) packages.

For example, if my code was:

#training model on training dataset
nnet_model <- nnet(Morphology~. ,size=10,data=morph_scaled_train, maxit=1500)
#generating predictions
nnet_prediction_prob <- predict(nnet_model,morph_test)

This gives the prediction output as numbers. I.e.

         Blue           Red           Green       Yellow
1   1.020685e-180  1.000000e+00 4.496185e-255 4.079526e-254
2   1.020685e-180  1.000000e+00 4.496185e-255 4.079526e-254
3   1.020685e-180  1.000000e+00 4.496185e-255 4.079526e-254

How could I convert this to a factor i.e. Row 1 is Red, row 2 is blue for example ... Giving the overall prediction. When I use predict function for other models e.g. random forest, logistic regression, it gives it as the overall prediction and not numbers, and this is what I wish for nnets. Is this possible? Or just the way the model works this cannot be interpreted. Thanks!


Solution

  • Use max.col:

    colnames(nnet_prediction_prob)[max.col(nnet_prediction_prob)]
    [1] "Red" "Red" "Red"