confusion matrix of bstTree predictions, Error: 'The data must contain some levels that overlap the reference.'
confusion matrix of bstTree predictions, Error: 'The data must contain some levels that overlap the reference.'
我正在尝试使用 bstTree 方法训练模型并打印出混淆矩阵。 adverse_effects 是我的 class 属性。
set.seed(1234)
splitIndex <- createDataPartition(attended_num_new_bstTree$adverse_effects, p = .80, list = FALSE, times = 1)
trainSplit <- attended_num_new_bstTree[ splitIndex,]
testSplit <- attended_num_new_bstTree[-splitIndex,]
ctrl <- trainControl(method = "cv", number = 5)
model_bstTree <- train(adverse_effects ~ ., data = trainSplit, method = "bstTree", trControl = ctrl)
predictors <- names(trainSplit)[names(trainSplit) != 'adverse_effects']
pred_bstTree <- predict(model_bstTree$finalModel, testSplit[,predictors])
plot.roc(auc_bstTree)
conf_bstTree= confusionMatrix(pred_bstTree,testSplit$adverse_effects)
但我收到错误消息“confusionMatrix.default(pred_bstTree, testSplit$adverse_effects) 中的错误:
数据必须包含一些与参考重叠的水平。'
max(pred_bstTree)
[1] 1.03385
min(pred_bstTree)
[1] 1.011738
> unique(trainSplit$adverse_effects)
[1] 0 1
Levels: 0 1
我该如何解决这个问题?
> head(trainSplit)
type New_missed Therapytypename New_Diesease gender adverse_effects change_in_exposure other_reasons other_medication
5 2 1 14 13 2 0 0 0 0
7 2 0 14 13 2 0 0 0 0
8 2 0 14 13 2 0 0 0 0
9 2 0 14 13 2 1 0 0 0
11 2 1 14 13 2 0 0 0 0
12 2 0 14 13 2 0 0 0 0
uvb_puva_type missed_prev_dose skintypeA skintypeB Age DoseB DoseA
5 5 1 1 1 22 3.000 0
7 5 0 1 1 22 4.320 0
8 5 0 1 1 22 4.752 0
9 5 0 1 1 22 5.000 0
11 5 1 1 1 22 5.000 0
12 5 0 1 1 22 5.000 0
max(pred_bstTree) [1] 1.03385
min(pred_bstTree) [1] 1.011738
错误说明了一切。绘制 ROC 只是检查不同阈值点的效果。基于阈值舍入发生例如0.7 将转换为 1 (TRUE class),0.3 将变为 0 (FALSE class);如果阈值是 0.5。阈值在 (0,1)
范围内
在您的情况下,无论阈值如何,您总是将所有观察结果变为 TRUE class,因为即使是最小预测也大于 1。(这就是为什么@phiver 想知道您是否正在做回归而不是 class化)。预测中没有任何零,'prediction' 中没有水平与 adverse_effects
中的零水平一致,因此出现此错误。
PS:如果不发布数据就很难找出错误的根本原因
我有类似的问题,指的是这个错误。我使用函数 confusionMatrix
:
confusionMatrix(actual, predicted, cutoff = 0.5)
我收到以下错误:Error in confusionMatrix.default(actual, predicted, cutoff = 0.5) : The data must contain some levels that overlap the reference.
我检查了几件事,例如:
class(actual)
-> 数值
class(predicted)
-> 整数
unique(actual)
-> 很多值,因为它是概率
unique(predicted)
-> 2 个级别:0 和 1
我得出结论,应用函数的截止部分有问题,所以我之前通过:
predicted<-ifelse(predicted> 0.5,1,0)
和 运行 confusionMatrix
函数,现在可以正常工作了:
cm<- confusionMatrix(actual, predicted)
cm$table
产生了正确的结果。
你的案例的一个要点,一旦你使代码工作,这可能会改善解释:
您混合了混淆矩阵的输入值(根据 confusionMatrix 包文档),而不是:
conf_bstTree= confusionMatrix(pred_bstTree,testSplit$adverse_effects)
你应该写:
conf_bstTree= confusionMatrix(testSplit$adverse_effects,pred_bstTree)
如前所述,一旦您找到使它起作用的方法,它很可能会帮助您解释混淆矩阵。
希望对您有所帮助。
我正在尝试使用 bstTree 方法训练模型并打印出混淆矩阵。 adverse_effects 是我的 class 属性。
set.seed(1234)
splitIndex <- createDataPartition(attended_num_new_bstTree$adverse_effects, p = .80, list = FALSE, times = 1)
trainSplit <- attended_num_new_bstTree[ splitIndex,]
testSplit <- attended_num_new_bstTree[-splitIndex,]
ctrl <- trainControl(method = "cv", number = 5)
model_bstTree <- train(adverse_effects ~ ., data = trainSplit, method = "bstTree", trControl = ctrl)
predictors <- names(trainSplit)[names(trainSplit) != 'adverse_effects']
pred_bstTree <- predict(model_bstTree$finalModel, testSplit[,predictors])
plot.roc(auc_bstTree)
conf_bstTree= confusionMatrix(pred_bstTree,testSplit$adverse_effects)
但我收到错误消息“confusionMatrix.default(pred_bstTree, testSplit$adverse_effects) 中的错误: 数据必须包含一些与参考重叠的水平。'
max(pred_bstTree)
[1] 1.03385
min(pred_bstTree)
[1] 1.011738
> unique(trainSplit$adverse_effects)
[1] 0 1
Levels: 0 1
我该如何解决这个问题?
> head(trainSplit)
type New_missed Therapytypename New_Diesease gender adverse_effects change_in_exposure other_reasons other_medication
5 2 1 14 13 2 0 0 0 0
7 2 0 14 13 2 0 0 0 0
8 2 0 14 13 2 0 0 0 0
9 2 0 14 13 2 1 0 0 0
11 2 1 14 13 2 0 0 0 0
12 2 0 14 13 2 0 0 0 0
uvb_puva_type missed_prev_dose skintypeA skintypeB Age DoseB DoseA
5 5 1 1 1 22 3.000 0
7 5 0 1 1 22 4.320 0
8 5 0 1 1 22 4.752 0
9 5 0 1 1 22 5.000 0
11 5 1 1 1 22 5.000 0
12 5 0 1 1 22 5.000 0
max(pred_bstTree) [1] 1.03385
min(pred_bstTree) [1] 1.011738
错误说明了一切。绘制 ROC 只是检查不同阈值点的效果。基于阈值舍入发生例如0.7 将转换为 1 (TRUE class),0.3 将变为 0 (FALSE class);如果阈值是 0.5。阈值在 (0,1)
范围内在您的情况下,无论阈值如何,您总是将所有观察结果变为 TRUE class,因为即使是最小预测也大于 1。(这就是为什么@phiver 想知道您是否正在做回归而不是 class化)。预测中没有任何零,'prediction' 中没有水平与 adverse_effects
中的零水平一致,因此出现此错误。
PS:如果不发布数据就很难找出错误的根本原因
我有类似的问题,指的是这个错误。我使用函数 confusionMatrix
:
confusionMatrix(actual, predicted, cutoff = 0.5)
我收到以下错误:Error in confusionMatrix.default(actual, predicted, cutoff = 0.5) : The data must contain some levels that overlap the reference.
我检查了几件事,例如:
class(actual)
-> 数值
class(predicted)
-> 整数
unique(actual)
-> 很多值,因为它是概率
unique(predicted)
-> 2 个级别:0 和 1
我得出结论,应用函数的截止部分有问题,所以我之前通过:
predicted<-ifelse(predicted> 0.5,1,0)
和 运行 confusionMatrix
函数,现在可以正常工作了:
cm<- confusionMatrix(actual, predicted)
cm$table
产生了正确的结果。
你的案例的一个要点,一旦你使代码工作,这可能会改善解释: 您混合了混淆矩阵的输入值(根据 confusionMatrix 包文档),而不是:
conf_bstTree= confusionMatrix(pred_bstTree,testSplit$adverse_effects)
你应该写:
conf_bstTree= confusionMatrix(testSplit$adverse_effects,pred_bstTree)
如前所述,一旦您找到使它起作用的方法,它很可能会帮助您解释混淆矩阵。
希望对您有所帮助。