在使用Caret机器学习包时,我遇到了Caret默认的“正面”结果选择问题,也就是在二元分类问题中结果因素的第一个级别。
该包表示可以将其设置为替代级别。有人能帮我定义正面结果吗?
谢谢。
看这个例子。从插入符号的示例扩展,加入了confusionMatrix。
lvs <- c("normal", "abnormal")
truth <- factor(rep(lvs, times = c(86, 258)),
levels = rev(lvs))
pred <- factor(
c(
rep(lvs, times = c(54, 32)),
rep(lvs, times = c(27, 231))),
levels = rev(lvs))
xtab <- table(pred, truth)
str(truth)
Factor w/ 2 levels "abnormal","normal": 2 2 2 2 2 2 2 2 2 2 ...
由于异常是第一级别,因此这将是默认的正类。
confusionMatrix(xtab)
Confusion Matrix and Statistics
truth
pred abnormal normal
abnormal 231 32
normal 27 54
Accuracy : 0.8285
95% CI : (0.7844, 0.8668)
No Information Rate : 0.75
P-Value [Acc > NIR] : 0.0003097
Kappa : 0.5336
Mcnemar's Test P-Value : 0.6025370
Sensitivity : 0.8953
Specificity : 0.6279
Pos Pred Value : 0.8783
Neg Pred Value : 0.6667
Prevalence : 0.7500
Detection Rate : 0.6715
Detection Prevalence : 0.7645
Balanced Accuracy : 0.7616
'Positive' Class : abnormal
要将正类更改为normal,只需在confusionMatrix中添加此内容。请注意与以前输出的差异,在灵敏度和其他计算中开始出现差异。
confusionMatrix(xtab, positive = "normal")
Confusion Matrix and Statistics
truth
pred abnormal normal
abnormal 231 32
normal 27 54
Accuracy : 0.8285
95% CI : (0.7844, 0.8668)
No Information Rate : 0.75
P-Value [Acc > NIR] : 0.0003097
Kappa : 0.5336
Mcnemar's Test P-Value : 0.6025370
Sensitivity : 0.6279
Specificity : 0.8953
Pos Pred Value : 0.6667
Neg Pred Value : 0.8783
Prevalence : 0.2500
Detection Rate : 0.1570
Detection Prevalence : 0.2355
Balanced Accuracy : 0.7616
'Positive' Class : normal
更改正类:
通过重新调整目标变量是一种有效的方法之一。
例如:在乳腺癌威斯康辛数据集中,诊断的默认级别是默认正类的基础。诊断的参考级别为:
cancer<-read.csv("breast-cancer-wisconsin.csv")
cancer$Diagnosis<-as.factor(cancer$Diagnosis)
levels(cancer$Diagnosis)
[1] "Benign" "Malignant"
在进行测试集和训练集划分以及模型拟合后,得到的混淆矩阵和性能指标如下:
Confusion Matrix and Statistics
predicted Actual
Benign Malignant
Benign 115 7
Malignant 2 80
Accuracy : 0.9559
95% CI : (0.9179, 0.9796)
No Information Rate : 0.5735
P-Value [Acc > NIR] : <2e-16
Kappa : 0.9091
Mcnemar's Test P-Value : 0.1824
Sensitivity : 0.9829
Specificity : 0.9195
Pos Pred Value : 0.9426
Neg Pred Value : 0.9756
Prevalence : 0.5735
Detection Rate : 0.5637
Detection Prevalence: 0.5980
Balanced Accuracy : 0.9512
'Positive' Class : Benign
需要注意的是,"Positive Class is Benign"表示阳性类别为良性。
使用relevel()
函数可以将阳性类别更改为"恶性"。该函数可以改变变量的参考水平。
cancer$Diagnosis <- relevel(cancer$Diagnosis, ref = "Malignant")
levels(cancer$Diagnosis)
[1] "Malignant" "Benign"
在进行测试训练拆分和模型拟合后,混淆矩阵性能准确度随参考值变化的结果为:
Confusion Matrix and Statistics
predicted Actual
Malignant Benign
Malignant 80 2
Benign 7 115
Accuracy : 0.9559
95% CI : (0.9179, 0.9796)
No Information Rate : 0.5735
P-Value [Acc > NIR] : <2e-16
Kappa : 0.9091
Mcnemar's Test P-Value : 0.1824
Sensitivity : 0.9195
Specificity : 0.9829
Pos Pred Value : 0.9756
Neg Pred Value : 0.9426
Prevalence : 0.4265
Detection Rate : 0.3922
Detection Prevalence : 0.4020
Balanced Accuracy : 0.9512
'Positive' Class : Malignant
这里的正类是恶性的
confusionMatrix(data = preds, reference = actual, positive="YOUR_POSITIVE_CLASS")
。 - Zahrafactor()
将DV提供给levels
参数排序,例如c("Positive", "Negative")
(如果它尚未是因子,则应为labels
,而levels
应为1:0
或任何使当前正值排在首位的内容)。 - DHW