如何逐步解决plot_confusion_matrix与plot_roc_curve的弃用警告?
解决scikit-learn中plot_confusion_matrix和plot_roc_curve的弃用警告
这两个警告是因为scikit-learn 1.0版本开始弃用了这两个独立函数,要求改用对应的类方法,具体替换步骤如下:
1. 替换plot_confusion_matrix
直接使用ConfusionMatrixDisplay.from_estimator方法替代,参数和原函数完全兼容:
首先导入对应类:
from sklearn.metrics import ConfusionMatrixDisplay
替换后的代码:
disp = ConfusionMatrixDisplay.from_estimator( xgb_clf, X_test, y_test, cmap='Blues', values_format='d', display_labels=['Default', 'Fully-Paid'] )
如果已经有模型预测结果y_pred,也可以用ConfusionMatrixDisplay.from_predictions(y_test, y_pred)来生成混淆矩阵。
2. 替换plot_roc_curve
使用RocCurveDisplay.from_estimator方法替代原函数:
先导入对应类:
from sklearn.metrics import RocCurveDisplay
替换后的代码:
disp = RocCurveDisplay.from_estimator( xgb_clf, X_test, y_test )
同样,若已有预测概率值y_score,可以用RocCurveDisplay.from_predictions(y_test, y_score)来生成ROC曲线。
替换完成后,运行代码就不会再出现弃用警告,同时代码也能兼容scikit-learn 1.2及以后的版本。
内容的提问来源于stack exchange,提问作者Michael
相关产品推荐
相关产品推荐

