我已经按照下面的代码在R中创建了XGBoost分类器
classifier8 = xgboost(data = as.matrix(training_set8[-10]), label = training_set8$Employee_Turnover, nrounds = 10)
现在,我需要为XGBoost创建一个混淆矩阵。
我已经在网上搜索,很遗憾找不到解决方案。
任何人都可以帮助我。
提前感谢
您可以使用caret::confusionMatrix()
功能,但是您需要对输出进行一些处理。显然,您需要一个真实结果的矢量(测试数据集),以将计算结果与真实结果进行比较:
# here the classical example of the package
library(xgboost)
# train dataset
data(agaricus.train, package='xgboost')
train <- agaricus.train
# test dataset
data(agaricus.test, package='xgboost')
test <- agaricus.test
# your model
bst <- xgboost(data = train$data, label = train$label, max.depth = 2,
eta = 1, nthread = 2, nround = 2, objective = "binary:logistic")
# you've to do your prediction here
pred <- predict(bst, test$data ,type = "response")
# and transform them in a 0 1 variable, you can choose the value to get 1
pred <- as.numeric(pred > 0.5)
library(caret)
confusionMatrix(factor(pred),factor(test$label))
Confusion Matrix and Statistics
Reference
Prediction 0 1
0 813 13
1 22 763
Accuracy : 0.9783
95% CI : (0.9699, 0.9848)
No Information Rate : 0.5183
P-Value [Acc > NIR] : <2e-16
Kappa : 0.9565
Mcnemar's Test P-Value : 0.1763
Sensitivity : 0.9737
Specificity : 0.9832
Pos Pred Value : 0.9843
Neg Pred Value : 0.9720
Prevalence : 0.5183
Detection Rate : 0.5047
Detection Prevalence : 0.5127
Balanced Accuracy : 0.9785
'Positive' Class : 0