将神经网络的概率转化为R中的预测

huangapple go评论66阅读模式
英文:

Converting neural net probablities into predictions in R

问题

请原谅我的无知,我正在学习模型背后的理论,特别是神经网络。我试图使用library(nnet) library(NeuralNetTools)包来训练一个模型。

例如,如果我的代码是:

# 在训练数据集上训练模型
nnet_model <- nnet(Morphology~. ,size=10,data=morph_scaled_train, maxit=1500)
# 生成预测
nnet_prediction_prob <- predict(nnet_model, morph_test)

这会给出数字形式的预测输出,即:

         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

我该如何将其转换为因子,即第一行是红色,第二行是蓝色,以此类推,给出总体预测。当我对其他模型(例如随机森林、逻辑回归)使用predict函数时,它会给出总体预测而不是数字,这正是我希望在神经网络中实现的。这是否可能?还是模型工作方式决定了无法解释这一点。谢谢!

英文:

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 &lt;- nnet(Morphology~. ,size=10,data=morph_scaled_train, maxit=1500)
#generating predictions
nnet_prediction_prob &lt;- 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!

答案1

得分: 2

Use max.col:

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

英文:

Use max.col:

colnames(nnet_prediction_prob)[max.col(nnet_prediction_prob)]
[1] &quot;Red&quot; &quot;Red&quot; &quot;Red&quot;

答案2

得分: 1

你可以在 predict 函数内添加 type = "class" 以获取类名。由于您没有提供任何数据,所以我在这里使用 iris 数据,如下所示:

library(nnet) 
library(NeuralNetTools)

# 在训练数据集上训练模型
nnet_model <- nnet(Species~. ,size=10,data=iris, maxit=1500)
# 生成预测
nnet_prediction <- predict(nnet_model, iris, type = "class")

这是您要求的代码的翻译部分。

英文:

You can add type = &quot;class&quot; within predict function to have the class names. You have not provided any data, so I am using iris data like

library(nnet) 
library(NeuralNetTools)

#training model on training dataset
nnet_model &lt;- nnet(Species~. ,size=10,data=iris, maxit=1500)
#generating predictions
nnet_prediction &lt;- predict(nnet_model, iris, type = &quot;class&quot;)

huangapple
  • 本文由 发表于 2023年4月4日 08:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定