calibration_curve#

sklearn.calibration.calibration_curve(y_true, y_prob, *, pos_label=None, n_bins=5, strategy='uniform')[source]#

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

此方法假设输入来自二元分类器,并将 [0, 1] 区间离散化为若干个 bin。

校准曲线也可能被称为可靠性图。

用户指南中阅读更多内容。

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

真实目标值。

y_prob形状为 (n_samples,) 的类数组

正类的概率。

pos_labelint, float, bool or str, default=None

正类的标签。

版本 1.1 中新增。

n_binsint, default=5

用于离散化 [0, 1] 区间的 bin 数量。较大的数字需要更多数据。不包含样本的 bin(即在 y_prob 中没有对应值的 bin)将不会被返回,因此返回的数组可能具有少于 n_bins 个值。

strategy{‘uniform’, ‘quantile’}, default=’uniform’

用于定义 bin 宽度的策略。

uniform

这些 bin 具有相同的宽度。

quantile

这些 bin 具有相同数量的样本,并取决于 y_prob

返回:
prob_true形状为 (n_bins,) 或更小的 ndarray

在每个 bin 中,样本属于正类的比例(正类分数)。

prob_pred形状为 (n_bins,) 或更小的 ndarray

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

另请参阅

CalibrationDisplay.from_predictions

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

CalibrationDisplay.from_estimator

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

References

Alexandru Niculescu-Mizil 和 Rich Caruana (2005) Predicting Good Probabilities With Supervised Learning, in Proceedings of the 22nd International Conference on Machine Learning (ICML). 参见第 4 节(对预测的定性分析)。

示例

>>> import numpy as np
>>> from sklearn.calibration import calibration_curve
>>> y_true = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1])
>>> y_pred = np.array([0.1, 0.2, 0.3, 0.4, 0.65, 0.7, 0.8, 0.9,  1.])
>>> prob_true, prob_pred = calibration_curve(y_true, y_pred, n_bins=3)
>>> prob_true
array([0. , 0.5, 1. ])
>>> prob_pred
array([0.2  , 0.525, 0.85 ])