r - 在gbm多项式分布中,如何使用predict得到分类输出?

标签 r machine-learning categorical-data multinomial gbm

我的响应是一个分类变量(一些字母),所以我在制作模型时使用了 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.

正如多米尼克建议的那样,您应该做的是通过执行 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

关于r - 在gbm多项式分布中,如何使用predict得到分类输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29454883/

相关文章:

R,dplyr : Function that quickly builds list of complementary rows based on conditions

r - 如何用逗号分割字符串但保留日期?

machine-learning - 基于手指弯曲对手部姿势进行分类的合适 ML 算法

label - 如何使用 spss 语法在输出中显示分类标签的数值

r - 如何在 R 中进行基于目标的编码

regex - 点元字符如何匹配换行符?

r - 在 ggvis 中创建包括国家边界的美国 map

machine-learning - Azure ML Tune 模型超参数

artificial-intelligence - 有哪些可定制的机器学习工具包可用?

r - R 中的分类变量 - R 选择哪一个作为引用?