混淆矩阵实战:Python sklearn 0.25.2 计算 5 大核心指标与可视化

📅 2026/7/4 9:52:14
混淆矩阵实战:Python sklearn 0.25.2 计算 5 大核心指标与可视化
混淆矩阵实战Python sklearn 0.25.2 计算 5 大核心指标与可视化在机器学习模型的评估过程中混淆矩阵就像一面照妖镜能清晰反映出分类器的真实表现。本文将带您用Python的sklearn 0.25.2版本从零开始构建完整的模型评估流程不仅计算Accuracy、Precision、Recall、F1和AUC这五大黄金指标还会展示两种专业级的可视化方法。1. 环境准备与数据生成首先确保您的Python环境已安装以下库pip install scikit-learn0.25.2 pandas matplotlib seaborn numpy我们模拟一个二分类问题的数据集from sklearn.datasets import make_classification import pandas as pd # 生成1000个样本的模拟数据 X, y make_classification( n_samples1000, n_features20, n_classes2, weights[0.7, 0.3], # 类别不平衡 random_state42 ) # 转换为DataFrame方便查看 df pd.DataFrame(X) df[target] y print(df[target].value_counts())提示在实际项目中建议使用train_test_split划分训练集和测试集这里为简化流程直接使用全数据2. 模型训练与预测我们选用随机森林作为示例模型from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split # 划分训练测试集 X_train, X_test, y_train, y_test train_test_split( X, y, test_size0.3, random_state42 ) # 训练模型 model RandomForestClassifier(n_estimators100, random_state42) model.fit(X_train, y_train) # 获取预测概率和类别 y_pred model.predict(X_test) y_proba model.predict_proba(X_test)[:, 1] # 正类概率3. 混淆矩阵核心指标计算sklearn的metrics模块提供了完整的评估工具链from sklearn.metrics import ( confusion_matrix, accuracy_score, precision_score, recall_score, f1_score, roc_auc_score ) # 计算混淆矩阵 cm confusion_matrix(y_test, y_pred) print(混淆矩阵\n, cm) # 计算五大核心指标 metrics { Accuracy: accuracy_score(y_test, y_pred), Precision: precision_score(y_test, y_pred), Recall: recall_score(y_test, y_pred), F1: f1_score(y_test, y_pred), AUC: roc_auc_score(y_test, y_proba) } pd.DataFrame.from_dict(metrics, orientindex, columns[Value])指标解释表指标名称计算公式适用场景Accuracy(TPTN)/(TPTNFPFN)类别平衡时总体评估PrecisionTP/(TPFP)注重预测准确性如垃圾邮件过滤RecallTP/(TPFN)注重查全率如疾病筛查F12*(Precision*Recall)/(PrecisionRecall)综合平衡Precision和RecallAUCROC曲线下面积评估模型整体区分能力4. 混淆矩阵可视化实战方法一Matplotlib热力图import matplotlib.pyplot as plt import numpy as np def plot_confusion_matrix(cm, classes): plt.figure(figsize(8,6)) plt.imshow(cm, interpolationnearest, cmapplt.cm.Blues) plt.title(Confusion Matrix) plt.colorbar() tick_marks np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation45) plt.yticks(tick_marks, classes) # 添加数值标注 thresh cm.max() / 2. for i in range(cm.shape[0]): for j in range(cm.shape[1]): plt.text(j, i, format(cm[i, j], d), hacenter, vacenter, colorwhite if cm[i, j] thresh else black) plt.ylabel(True label) plt.xlabel(Predicted label) plt.tight_layout() plot_confusion_matrix(cm, classes[Negative, Positive]) plt.show()方法二Seaborn高级可视化import seaborn as sns def plot_seaborn_matrix(cm, classes): plt.figure(figsize(8,6)) sns.heatmap(cm, annotTrue, fmtd, cmapBlues, xticklabelsclasses, yticklabelsclasses) plt.title(Seaborn Confusion Matrix) plt.ylabel(True Label) plt.xlabel(Predicted Label) plt.show() plot_seaborn_matrix(cm, classes[Negative, Positive])两种可视化对比特性Matplotlib版本Seaborn版本代码复杂度中等简单自定义程度高中等美观度基础专业交互性无无适用场景需要高度定制时快速生成专业图表5. 进阶技巧与实战建议5.1 多分类问题处理对于超过两个类别的问题sklearn同样支持from sklearn.metrics import multilabel_confusion_matrix # 假设我们有一个三分类问题 y_true_multi [0, 1, 2, 0, 1, 2] y_pred_multi [0, 2, 1, 0, 0, 1] mcm multilabel_confusion_matrix(y_true_multi, y_pred_multi) print(多分类混淆矩阵\n, mcm)5.2 样本权重调整当数据存在严重不平衡时# 计算带权重的指标 weighted_precision precision_score(y_test, y_pred, averageweighted) weighted_recall recall_score(y_test, y_pred, averageweighted) print(f加权Precision: {weighted_precision:.4f}) print(f加权Recall: {weighted_recall:.4f})5.3 阈值优化通过调整分类阈值可以优化特定指标from sklearn.metrics import precision_recall_curve precisions, recalls, thresholds precision_recall_curve(y_test, y_proba) plt.figure(figsize(10,6)) plt.plot(thresholds, precisions[:-1], b--, labelPrecision) plt.plot(thresholds, recalls[:-1], g-, labelRecall) plt.xlabel(Threshold) plt.legend(loccenter left) plt.title(Precision-Recall vs Threshold) plt.show()在实际项目中我发现混淆矩阵配合ROC曲线能最全面地评估模型性能。特别是在金融风控领域通过调整阈值可以在风险覆盖率和误报率之间找到最佳平衡点。