【发布时间】: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