混淆矩阵显示#
- class sklearn.metrics.ConfusionMatrixDisplay(confusion_matrix, *, display_labels=None)[source]#
- 混淆矩阵可视化。 - 建议使用 - from_estimator或- from_predictions创建- ConfusionMatrixDisplay。所有参数都存储为属性。- 更多信息请参见 用户指南。 - 参数:
- confusion_matrix形状为 (n_classes, n_classes) 的 ndarray
- 混淆矩阵。 
- display_labels形状为 (n_classes,) 的 ndarray,默认为 None
- 绘图显示标签。如果为 None,则显示标签设置为从 0 到 - n_classes - 1。
 
- 属性:
- im_matplotlib AxesImage
- 表示混淆矩阵的图像。 
- text_形状为 (n_classes, n_classes) 的 ndarray,数据类型为 matplotlib Text,或 None
- Matplotlib 坐标轴数组。 - None如果- include_values为假。
- ax_matplotlib Axes
- 包含混淆矩阵的坐标轴。 
- figure_matplotlib Figure
- 包含混淆矩阵的图形。 
 
 - 另请参见 - confusion_matrix
- 计算混淆矩阵以评估分类的准确性。 
- ConfusionMatrixDisplay.from_estimator
- 根据估计器、数据和标签绘制混淆矩阵。 
- ConfusionMatrixDisplay.from_predictions
- 根据真实标签和预测标签绘制混淆矩阵。 
 - 示例 - >>> import matplotlib.pyplot as plt >>> from sklearn.datasets import make_classification >>> from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay >>> from sklearn.model_selection import train_test_split >>> from sklearn.svm import SVC >>> X, y = make_classification(random_state=0) >>> X_train, X_test, y_train, y_test = train_test_split(X, y, ... random_state=0) >>> clf = SVC(random_state=0) >>> clf.fit(X_train, y_train) SVC(random_state=0) >>> predictions = clf.predict(X_test) >>> cm = confusion_matrix(y_test, predictions, labels=clf.classes_) >>> disp = ConfusionMatrixDisplay(confusion_matrix=cm, ... display_labels=clf.classes_) >>> disp.plot() <...> >>> plt.show()   - 类方法 from_estimator(estimator, X, y, *, labels=None, sample_weight=None, normalize=None, display_labels=None, include_values=True, xticks_rotation='horizontal', values_format=None, cmap='viridis', ax=None, colorbar=True, im_kw=None, text_kw=None)[source]#
- 根据估计器和一些数据绘制混淆矩阵。 - 在用户指南中了解更多信息。 - 1.0 版本中新增。 - 参数:
- estimator估计器实例
- 已拟合的分类器或已拟合的 - Pipeline(其中最后一个估计器是分类器)。
- X形状为 (n_samples, n_features) 的{数组、稀疏矩阵}
- 输入值。 
- y形状为 (n_samples,) 的数组
- 目标值。 
- labels形状为 (n_classes,) 的数组,默认为 None
- 用于索引混淆矩阵的标签列表。这可用于重新排序或选择标签的子集。如果给出 - None,则使用在- y_true或- y_pred中至少出现一次的标签(按排序顺序)。
- sample_weight形状为 (n_samples,) 的数组,默认为 None
- 样本权重。 
- normalize{'true', 'pred', 'all'},默认为 None
- 是否规范化矩阵中显示的计数 - 如果 - 'true',则混淆矩阵根据真实情况(例如,行)进行归一化;
- 如果 - 'pred',则混淆矩阵根据预测情况(例如,列)进行归一化;
- 如果 - 'all',则混淆矩阵根据样本总数进行归一化;
- 如果 - None(默认值),则混淆矩阵不会被归一化。
 
- display_labels形状为 (n_classes,) 的数组,默认为 None
- 用于绘图的目标名称。默认情况下,如果定义了 - labels,则使用它;否则,将使用- y_true和- y_pred的唯一标签。
- include_values布尔值,默认为 True
- 包含混淆矩阵中的值。 
- xticks_rotation{'vertical', 'horizontal'} 或浮点数,默认为 'horizontal'
- x 轴刻度标签的旋转。 
- values_format字符串,默认为 None
- 混淆矩阵中值的格式规范。如果为 - None,则格式规范为'd'或'.2g',两者中较短者。
- cmap字符串或 matplotlib 颜色图,默认为 'viridis'
- matplotlib 识别的颜色图。 
- axmatplotlib Axes,默认为 None
- 要绘制的 Axes 对象。如果为 - None,则创建一个新的图形和 Axes。
- colorbar布尔值,默认为 True
- 是否向绘图添加颜色条。 
- im_kw字典,默认为 None
- 传递给 - matplotlib.pyplot.imshow调用的关键字字典。
- text_kw字典,默认为 None
- 传递给 - matplotlib.pyplot.text调用的关键字字典。- 1.2 版本中新增。 
 
- 返回:
- displayConfusionMatrixDisplay
 
- display
 - 另请参见 - ConfusionMatrixDisplay.from_predictions
- 根据真实标签和预测标签绘制混淆矩阵。 
 - 示例 - >>> import matplotlib.pyplot as plt >>> from sklearn.datasets import make_classification >>> from sklearn.metrics import ConfusionMatrixDisplay >>> from sklearn.model_selection import train_test_split >>> from sklearn.svm import SVC >>> X, y = make_classification(random_state=0) >>> X_train, X_test, y_train, y_test = train_test_split( ... X, y, random_state=0) >>> clf = SVC(random_state=0) >>> clf.fit(X_train, y_train) SVC(random_state=0) >>> ConfusionMatrixDisplay.from_estimator( ... clf, X_test, y_test) <...> >>> plt.show()   
 - 类方法 from_predictions(y_true, y_pred, *, labels=None, sample_weight=None, normalize=None, display_labels=None, include_values=True, xticks_rotation='horizontal', values_format=None, cmap='viridis', ax=None, colorbar=True, im_kw=None, text_kw=None)[source]#
- 根据真实标签和预测标签绘制混淆矩阵。 - 在用户指南中了解更多信息。 - 1.0 版本中新增。 - 参数:
- y_true形状为 (n_samples,) 的数组
- 真实标签。 
- y_predarray-like 形状 (n_samples,)
- 分类器 - predict方法预测的标签。
- labels形状为 (n_classes,) 的数组,默认为 None
- 用于索引混淆矩阵的标签列表。这可用于重新排序或选择标签的子集。如果给出 - None,则使用在- y_true或- y_pred中至少出现一次的标签(按排序顺序)。
- sample_weight形状为 (n_samples,) 的数组,默认为 None
- 样本权重。 
- normalize{'true', 'pred', 'all'},默认为 None
- 是否规范化矩阵中显示的计数 - 如果 - 'true',则混淆矩阵根据真实情况(例如,行)进行归一化;
- 如果 - 'pred',则混淆矩阵根据预测情况(例如,列)进行归一化;
- 如果 - 'all',则混淆矩阵根据样本总数进行归一化;
- 如果 - None(默认值),则混淆矩阵不会被归一化。
 
- display_labels形状为 (n_classes,) 的数组,默认为 None
- 用于绘图的目标名称。默认情况下,如果定义了 - labels,则使用它;否则,将使用- y_true和- y_pred的唯一标签。
- include_values布尔值,默认为 True
- 包含混淆矩阵中的值。 
- xticks_rotation{'vertical', 'horizontal'} 或浮点数,默认为 'horizontal'
- x 轴刻度标签的旋转。 
- values_format字符串,默认为 None
- 混淆矩阵中值的格式规范。如果为 - None,则格式规范为'd'或'.2g',两者中较短者。
- cmap字符串或 matplotlib 颜色图,默认为 'viridis'
- matplotlib 识别的颜色图。 
- axmatplotlib Axes,默认为 None
- 要绘制的 Axes 对象。如果为 - None,则创建一个新的图形和 Axes。
- colorbar布尔值,默认为 True
- 是否向绘图添加颜色条。 
- im_kw字典,默认为 None
- 传递给 - matplotlib.pyplot.imshow调用的关键字字典。
- text_kw字典,默认为 None
- 传递给 - matplotlib.pyplot.text调用的关键字字典。- 1.2 版本中新增。 
 
- 返回:
- displayConfusionMatrixDisplay
 
- display
 - 另请参见 - ConfusionMatrixDisplay.from_estimator
- 根据估计器、数据和标签绘制混淆矩阵。 
 - 示例 - >>> import matplotlib.pyplot as plt >>> from sklearn.datasets import make_classification >>> from sklearn.metrics import ConfusionMatrixDisplay >>> from sklearn.model_selection import train_test_split >>> from sklearn.svm import SVC >>> X, y = make_classification(random_state=0) >>> X_train, X_test, y_train, y_test = train_test_split( ... X, y, random_state=0) >>> clf = SVC(random_state=0) >>> clf.fit(X_train, y_train) SVC(random_state=0) >>> y_pred = clf.predict(X_test) >>> ConfusionMatrixDisplay.from_predictions( ... y_test, y_pred) <...> >>> plt.show()   
 - plot(*, include_values=True, cmap='viridis', xticks_rotation='horizontal', values_format=None, ax=None, colorbar=True, im_kw=None, text_kw=None)[source]#
- 绘制可视化结果。 - 参数:
- include_values布尔值,默认为 True
- 包含混淆矩阵中的值。 
- cmap字符串或 matplotlib 颜色图,默认为 'viridis'
- matplotlib 识别的颜色图。 
- xticks_rotation{'vertical', 'horizontal'} 或浮点数,默认为 'horizontal'
- x 轴刻度标签的旋转。 
- values_format字符串,默认为 None
- 混淆矩阵中值的格式规范。如果为 - None,则格式规范为'd'或'.2g',两者中较短者。
- axmatplotlib 坐标轴,默认为 None
- 要绘制的 Axes 对象。如果为 - None,则创建一个新的图形和 Axes。
- colorbar布尔值,默认为 True
- 是否向绘图添加颜色条。 
- im_kw字典,默认为 None
- 传递给 - matplotlib.pyplot.imshow调用的关键字字典。
- text_kw字典,默认为 None
- 传递给 - matplotlib.pyplot.text调用的关键字字典。- 1.2 版本中新增。 
 
- 返回:
- displayConfusionMatrixDisplay
- 返回一个包含绘制混淆矩阵所有信息的 - ConfusionMatrixDisplay实例。
 
- display
 
 
 
     
 
 
 
 
 
