use*_*602 2 r classification machine-learning
我在R中训练了一个gbm模型.由于我试图预测一个非常罕见的情况,我得到了很多误报.我想将正("好")案例的阈值从默认值更改为0.7.到目前为止,这是我的代码.
modFit.glm.ml <- train(as.factor(ml.training$one_lease)~., data=ml.training, method = "glm")
confusionMatrix(ml.testing$one_lease, predict(modFit.glm.ml, ml.testing), positive = "Good")
Run Code Online (Sandbox Code Playgroud)
此代码有效但它使用默认截止值.有人提到这可能与预测功能,但我不知道如何做到这一点.
您没有提供可重现的示例,因此这里使用iris数据集来预测虹膜是否为setosa类型:
dat <- iris
dat$positive <- as.factor(ifelse(dat$Species == "setosa", "s", "ns"))
library(caret)
mod <- train(positive~Sepal.Length, data=dat, method="glm")
Run Code Online (Sandbox Code Playgroud)
要使用除0.5之外的预测概率的截止值生成混淆矩阵,您可以predict使用您想要的任何截止值来阈值函数返回的概率:
confusionMatrix(table(predict(mod, type="prob")[,"s"] >= 0.25,
dat$positive == "s"))
# Confusion Matrix and Statistics
#
#
# FALSE TRUE
# FALSE 88 3
# TRUE 12 47
#
# Accuracy : 0.9
# 95% CI : (0.8404, 0.9429)
# No Information Rate : 0.6667
# P-Value [Acc > NIR] : 2.439e-11
#
# Kappa : 0.7847
# Mcnemar's Test P-Value : 0.03887
#
# Sensitivity : 0.8800
# Specificity : 0.9400
# Pos Pred Value : 0.9670
# Neg Pred Value : 0.7966
# Prevalence : 0.6667
# Detection Rate : 0.5867
# Detection Prevalence : 0.6067
# Balanced Accuracy : 0.9100
#
# 'Positive' Class : FALSE
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6122 次 |
| 最近记录: |