在gbm multinomial dist中,如何使用predict来得到分类输出?

In gbm multinomial dist, how to use predict to get categorical output?

我的响应是一个分类变量(一些字母),所以我在制作模型时使用了 distribution='multinomial',现在我想根据这些字母预测响应并获得输出,而不是概率矩阵。

然而在predict(model, newdata, type='response')中,它给出了概率,与type='link'的结果相同。

有没有办法获得分类输出?

BST = gbm(V1~.,data=training,distribution='multinomial',n.trees=2000,interaction.depth=4,cv.folds=5,shrinkage=0.005)

predBST = predict(BST,newdata=test,type='response')

predict.gbm文档中提到:

If type="response" then gbm converts back to the same scale as the outcome. Currently the only effect this will have is returning probabilities for bernoulli and expected counts for poisson. For the other distributions "response" and "link" return the same.

正如 Dominic 所建议的,您应该做的是通过对预测的向量输出执行 apply(.., 1, which.max),从结果 predBST 矩阵中选择概率最高的响应。 以下是 iris 数据集的代码示例:

library(gbm)

data(iris)

df <- iris[,-c(1)] # remove index

df <- df[sample(nrow(df)),]  # shuffle

df.train <- df[1:100,]
df.test <- df[101:150,]

BST = gbm(Species~.,data=df.train,
         distribution='multinomial',
         n.trees=200,
         interaction.depth=4,
         #cv.folds=5,
         shrinkage=0.005)

predBST = predict(BST,n.trees=200, newdata=df.test,type='response')

p.predBST <- apply(predBST, 1, which.max)

> predBST[1:6,,]
     setosa versicolor  virginica
[1,] 0.89010862 0.05501921 0.05487217
[2,] 0.09370400 0.45616148 0.45013452
[3,] 0.05476228 0.05968445 0.88555327
[4,] 0.05452803 0.06006513 0.88540684
[5,] 0.05393377 0.06735331 0.87871292
[6,] 0.05416855 0.06548646 0.88034499

 > head(p.predBST)
 [1] 1 2 3 3 3 3