CalibrationDisplay#

class sklearn.calibration.CalibrationDisplay(prob_true, prob_pred, y_prob, *, estimator_name=None, pos_label=None)[源码]#

校准曲线(也称为可靠性图)可视化。

建议使用 from_estimatorfrom_predictions 来创建 CalibrationDisplay。所有参数都存储为属性。

用户指南 中了解更多关于校准的信息,在 可视化 中了解更多关于 scikit-learn 可视化 API 的信息。

有关如何使用此可视化的示例,请参阅 概率校准曲线

在 1.0 版本中新增。

参数:
prob_true形状为 (n_bins,) 的 ndarray

在每个 bin 中,其类别为正类的样本比例(正类分数)。

prob_pred形状为 (n_bins,) 的 ndarray

每个 bin 中的平均预测概率。

y_prob形状为 (n_samples,) 的 ndarray

每个样本的正类的概率估计。

estimator_namestr,默认为 None

估计器名称。如果为 None,则不显示估计器名称。

pos_labelint, float, bool 或 str,默认为 None

计算校准曲线时的正类。默认情况下,使用 from_estimatorpos_label 设置为 estimators.classes_[1],使用 from_predictions 时设置为 1。

在 1.1 版本中新增。

属性:
line_matplotlib Artist

校准曲线。

ax_matplotlib Axes

包含校准曲线的坐标轴。

figure_matplotlib Figure

包含曲线的图。

另请参阅

calibration_curve

计算校准曲线的真实概率和预测概率。

CalibrationDisplay.from_predictions

使用真实标签和预测标签绘制校准曲线。

CalibrationDisplay.from_estimator

使用估计器和数据绘制校准曲线。

示例

>>> from sklearn.datasets import make_classification
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.calibration import calibration_curve, CalibrationDisplay
>>> X, y = make_classification(random_state=0)
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, random_state=0)
>>> clf = LogisticRegression(random_state=0)
>>> clf.fit(X_train, y_train)
LogisticRegression(random_state=0)
>>> y_prob = clf.predict_proba(X_test)[:, 1]
>>> prob_true, prob_pred = calibration_curve(y_test, y_prob, n_bins=10)
>>> disp = CalibrationDisplay(prob_true, prob_pred, y_prob)
>>> disp.plot()
<...>
classmethod from_estimator(estimator, X, y, *, n_bins=5, strategy='uniform', pos_label=None, name=None, ax=None, ref_line=True, **kwargs)[源码]#

使用二元分类器和数据绘制校准曲线。

校准曲线,也称为可靠性图,使用二元分类器的输入,并将每个 bin 的平均预测概率与正类分数绘制在 y 轴上。

额外的关键字参数将被传递给 matplotlib.pyplot.plot

用户指南 中了解更多关于校准的信息,在 可视化 中了解更多关于 scikit-learn 可视化 API 的信息。

在 1.0 版本中新增。

参数:
estimator估计器实例

已拟合的分类器或已拟合的 Pipeline,其中最后一个估计器是分类器。该分类器必须具有 predict_proba 方法。

X形状为 (n_samples, n_features) 的 {array-like, 稀疏矩阵}

输入值。

y形状为 (n_samples,) 的 array-like

二元目标值。

n_binsint,默认为 5

计算校准曲线时,将 [0, 1] 区间离散化为的 bin 数量。数字越大,需要的数据越多。

strategy{‘uniform’, ‘quantile’},默认为 ‘uniform’

用于定义 bin 宽度的策略。

  • 'uniform': bin 具有相同的宽度。

  • 'quantile': bin 具有相同的样本数量,并取决于预测概率。

pos_labelint, float, bool 或 str,默认为 None

计算校准曲线时的正类。默认情况下,estimators.classes_[1] 被视为正类。

在 1.1 版本中新增。

namestr,默认为 None

曲线的标签名称。如果为 None,则使用估计器的名称。

axmatplotlib 坐标轴,默认为 None

绘图所用的坐标轴对象。如果为 None,则会创建新的图形和坐标轴。

ref_linebool,默认为 True

如果为 True,则绘制一条表示完美校准分类器的参考线。

**kwargsdict

将传递给 matplotlib.pyplot.plot 的关键字参数。

返回:
displayCalibrationDisplay

存储计算值的对象。

另请参阅

CalibrationDisplay.from_predictions

使用真实标签和预测标签绘制校准曲线。

示例

>>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import make_classification
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.calibration import CalibrationDisplay
>>> X, y = make_classification(random_state=0)
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, random_state=0)
>>> clf = LogisticRegression(random_state=0)
>>> clf.fit(X_train, y_train)
LogisticRegression(random_state=0)
>>> disp = CalibrationDisplay.from_estimator(clf, X_test, y_test)
>>> plt.show()
../../_images/sklearn-calibration-CalibrationDisplay-1.png
classmethod from_predictions(y_true, y_prob, *, n_bins=5, strategy='uniform', pos_label=None, name=None, ax=None, ref_line=True, **kwargs)[源码]#

使用真实标签和预测概率绘制校准曲线。

校准曲线,也称为可靠性图,使用二元分类器的输入,并将每个 bin 的平均预测概率与正类分数绘制在 y 轴上。

额外的关键字参数将被传递给 matplotlib.pyplot.plot

用户指南 中了解更多关于校准的信息,在 可视化 中了解更多关于 scikit-learn 可视化 API 的信息。

在 1.0 版本中新增。

参数:
y_true形状为 (n_samples,) 的 array-like

真实标签。

y_prob形状为 (n_samples,) 的 array-like

正类的预测概率。

n_binsint,默认为 5

计算校准曲线时,将 [0, 1] 区间离散化为的 bin 数量。数字越大,需要的数据越多。

strategy{‘uniform’, ‘quantile’},默认为 ‘uniform’

用于定义 bin 宽度的策略。

  • 'uniform': bin 具有相同的宽度。

  • 'quantile': bin 具有相同的样本数量,并取决于预测概率。

pos_labelint, float, bool 或 str,默认为 None

计算校准曲线时的正类。默认情况下 pos_label 设置为 1。

在 1.1 版本中新增。

namestr,默认为 None

曲线的标签名称。

axmatplotlib 坐标轴,默认为 None

绘图所用的坐标轴对象。如果为 None,则会创建新的图形和坐标轴。

ref_linebool,默认为 True

如果为 True,则绘制一条表示完美校准分类器的参考线。

**kwargsdict

将传递给 matplotlib.pyplot.plot 的关键字参数。

返回:
displayCalibrationDisplay

存储计算值的对象。

另请参阅

CalibrationDisplay.from_estimator

使用估计器和数据绘制校准曲线。

示例

>>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import make_classification
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.calibration import CalibrationDisplay
>>> X, y = make_classification(random_state=0)
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, random_state=0)
>>> clf = LogisticRegression(random_state=0)
>>> clf.fit(X_train, y_train)
LogisticRegression(random_state=0)
>>> y_prob = clf.predict_proba(X_test)[:, 1]
>>> disp = CalibrationDisplay.from_predictions(y_test, y_prob)
>>> plt.show()
../../_images/sklearn-calibration-CalibrationDisplay-2.png
plot(*, ax=None, name=None, ref_line=True, **kwargs)[源码]#

绘制可视化图。

额外的关键字参数将被传递给 matplotlib.pyplot.plot

参数:
axMatplotlib 坐标轴,默认为 None

绘图所用的坐标轴对象。如果为 None,则会创建新的图形和坐标轴。

namestr,默认为 None

曲线的标签名称。如果为 None,则在 estimator_name 不为 None 时使用 estimator_name,否则不显示标签。

ref_linebool,默认为 True

如果为 True,则绘制一条表示完美校准分类器的参考线。

**kwargsdict

将传递给 matplotlib.pyplot.plot 的关键字参数。

返回:
displayCalibrationDisplay

存储计算值的对象。