【问题标题】:How do I create a gain chart in R for a decision tree model?如何在 R 中为决策树模型创建增益图?
【发布时间】:2015-02-08 02:19:56
【问题描述】:

我在 R 中创建了一个决策树模型。目标变量是 Salary,我们试图根据其他输入变量来预测一个人的薪水是高于还是低于 50k

df<-salary.data 

train = sample(1:nrow(df), nrow(df)/2)
train = sample(1:nrow(df), size=0.2*nrow(df))
test = - train
training_data = df[train, ]
testing_data = df[test, ]

fit <- rpart(training_data$INCOME ~ ., method="class", data=training_data)##generate tree
testing_data$predictionsOutput = predict(fit, newdata=testing_data, type="class")##make prediction

之后我尝试通过执行以下操作来创建增益图表

# Gain Chart
pred <- prediction(testing_data$predictionsOutput, testing_data$INCOME)
gain <- performance(pred,"tpr","fpr")
plot(gain, col="orange", lwd=2)

通过查看参考资料,我无法理解如何使用 ROCR 包通过“预测”功能构建图表。这仅适用于二进制目标变量吗?我收到错误消息“预测格式无效”

对于帮助我为上述模型构建增益图表的任何帮助,我们将不胜感激。谢谢!!

  AGE          EMPLOYER     DEGREE             MSTATUS            JOBTYPE     SEX C.GAIN C.LOSS HOURS
1  39         State-gov  Bachelors       Never-married       Adm-clerical    Male   2174      0    40
2  50  Self-emp-not-inc  Bachelors  Married-civ-spouse    Exec-managerial    Male      0      0    13
3  38           Private    HS-grad            Divorced  Handlers-cleaners    Male      0      0    40

         COUNTRY INCOME
1  United-States  <=50K
2  United-States  <=50K
3  United-States  <=50K

【问题讨论】:

  • 根据文档,Currently, ROCR supports only binary classification ... If there are more than two distinct label symbols, execution stops with an error message.
  • 在这种情况下,它是一个二进制分类,目标是 >50k 或
  • 好的。但是prediction 的第二个参数必须只有两个值。 testing_data$INCOME 是否只有两个值?
  • 是的, testing_data$INCOME 只有两个潜在值,>50k 或
  • 你能提供salary.data吗?在您的问题中发布dput(salary.data) 的输出,或者,如果它太大,请将其上传到某处并发布链接。

标签: r machine-learning classification rstudio decision-tree


【解决方案1】:

如果你改变这应该可以工作

predict(fit, newdata=testing_data, type="class")

predict(fit, newdata=testing_data, type="prob")

增益图表希望按模型概率排序。

【讨论】:

    【解决方案2】:

    使用 c() 将预测转换为向量

    library('rpart')
    library('ROCR')
    setwd('C:\\Users\\John\\Google Drive\\working\\R\\questions')
    df<-read.csv(file='salary-class.csv',header=TRUE)
    
    train = sample(1:nrow(df), nrow(df)/2)
    train = sample(1:nrow(df), size=0.2*nrow(df))
    test = - train
    training_data = df[train, ]
    testing_data = df[test, ]
    
    fit <- rpart(training_data$INCOME ~ ., method="class", data=training_data)##generate tree
    testing_data$predictionsOutput = predict(fit, 
                                             newdata=testing_data, type="class")##make prediction
    
    # Doesn't work
    # pred <- prediction(testing_data$predictionsOutput, testing_data$INCOME)
    v <- c(pred = testing_data$predictionsOutput)
    pred <- prediction(v, testing_data$INCOME)
    gain <- performance(pred,"tpr","fpr")
    plot(gain, col="orange", lwd=2)
    

    【讨论】:

    猜你喜欢
    • 2011-06-01
    • 2014-05-09
    • 2021-12-10
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 2019-10-02
    • 2014-01-07
    • 2015-12-03
    相关资源
    最近更新 更多