LearningCurveDisplay#

class sklearn.model_selection.LearningCurveDisplay(*, train_sizes, train_scores, test_scores, score_name=None)[source]#

学习曲线可视化。

建议使用 from_estimator 来创建 LearningCurveDisplay 实例。所有参数都存储为属性。

有关可视化 API 的一般信息,请参阅 用户指南,有关学习曲线可视化的 详细文档

1.2 版本新增。

参数:
train_sizesndarray of shape (n_unique_ticks,)

用于生成学习曲线的训练示例数量。

train_scoresndarray of shape (n_ticks, n_cv_folds)

训练集上的得分。

test_scoresndarray of shape (n_ticks, n_cv_folds)

测试集上的得分。

score_namestr, default=None

learning_curve 中使用的分数的名称。它将覆盖从 scoring 参数推断出的名称。如果 scoreNone,则如果 negate_scoreFalse,我们使用 "Score",否则使用 "Negative score"。如果 scoring 是字符串或可调用对象,我们推断名称。我们将 _ 替换为空格并大写首字母。如果 negate_scoreFalse,我们删除 neg_ 并替换为 "Negative",否则直接删除它。

属性:
ax_matplotlib Axes

包含学习曲线的轴。

figure_matplotlib Figure

包含学习曲线的图。

errorbar_list of matplotlib Artist or None

std_display_style"errorbar" 时,这是一个 matplotlib.container.ErrorbarContainer 对象的列表。如果使用其他样式,errorbar_None

lines_list of matplotlib Artist or None

std_display_style"fill_between" 时,这是一个对应于平均训练分数和测试分数的 matplotlib.lines.Line2D 对象的列表。如果使用其他样式,line_None

fill_between_list of matplotlib Artist or None

std_display_style"fill_between" 时,这是一个 matplotlib.collections.PolyCollection 对象的列表。如果使用其他样式,fill_between_None

另请参阅

sklearn.model_selection.learning_curve

计算学习曲线。

示例

>>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import LearningCurveDisplay, learning_curve
>>> from sklearn.tree import DecisionTreeClassifier
>>> X, y = load_iris(return_X_y=True)
>>> tree = DecisionTreeClassifier(random_state=0)
>>> train_sizes, train_scores, test_scores = learning_curve(
...     tree, X, y)
>>> display = LearningCurveDisplay(train_sizes=train_sizes,
...     train_scores=train_scores, test_scores=test_scores, score_name="Score")
>>> display.plot()
<...>
>>> plt.show()
../../_images/sklearn-model_selection-LearningCurveDisplay-1.png
classmethod from_estimator(estimator, X, y, *, groups=None, train_sizes=array([0.1, 0.33, 0.55, 0.78, 1.]), cv=None, scoring=None, exploit_incremental_learning=False, n_jobs=None, pre_dispatch='all', verbose=0, shuffle=False, random_state=None, error_score=nan, fit_params=None, ax=None, negate_score=False, score_name=None, score_type='both', std_display_style='fill_between', line_kw=None, fill_between_kw=None, errorbar_kw=None)[source]#

从估计器创建学习曲线显示。

有关可视化 API 的一般信息,请参阅 用户指南,有关学习曲线可视化的 详细文档

参数:
estimator实现“fit”和“predict”方法的对象类型

每次验证都会克隆该类型的对象。

Xshape 为 (n_samples, n_features) 的 array-like

训练数据,其中 n_samples 是样本数,n_features 是特征数。

y形状为 (n_samples,) 或 (n_samples, n_outputs) 的类似数组或 None

相对于 X 的分类或回归目标;对于无监督学习为 None。

groups形状为 (n_samples,) 的类数组对象,默认=None

在将数据集拆分为训练/测试集时使用的样本组标签。仅与“Group”cv 实例(例如 GroupKFold)结合使用。

train_sizesarray-like of shape (n_ticks,), default=np.linspace(0.1, 1.0, 5)

用于生成学习曲线的训练示例的相对或绝对数量。如果 dtype 是 float,则将其视为训练集最大大小(由所选验证方法确定)的一部分,即它必须在 (0, 1] 范围内。否则,它被解释为训练集的绝对大小。请注意,对于分类,样本数量通常必须足够大,以包含每个类别至少一个样本。

cvint, cross-validation generator or an iterable, default=None

确定交叉验证拆分策略。cv 的可能输入包括

  • None,使用默认的 5 折交叉验证,

  • int,指定 (Stratified)KFold 中的折叠数,

  • CV 分割器,

  • 一个可迭代对象,产生索引数组形式的 (训练集, 测试集) 拆分。

对于 int/None 输入,如果估计器是分类器且 y 是二元或多类别,则使用 StratifiedKFold。在所有其他情况下,使用 KFold。这些分割器以 shuffle=False 实例化,因此分割在不同调用中将相同。

有关此处可使用的各种交叉验证策略,请参阅 用户指南

scoringstr 或可调用对象,默认=None

计算学习曲线时使用的评分方法。选项

exploit_incremental_learningbool, default=False

如果估计器支持增量学习,这将用于加快不同训练集大小的拟合速度。

n_jobsint, default=None

并行运行的作业数。估计器的训练和得分计算在不同的训练集和测试集上并行化。None 表示 1,除非在 joblib.parallel_backend 上下文中。-1 表示使用所有处理器。有关详细信息,请参阅词汇表

pre_dispatchint or str, default=’all’

并行执行的预调度作业数(默认为全部)。该选项可以减少分配的内存。该字符串可以是像“2*n_jobs”这样的表达式。

verboseint, default=0

控制详细程度:值越高,消息越多。

shufflebool, default=False

是否根据 `train_sizes` 取前缀之前打乱训练数据。

random_stateint, RandomState instance or None, default=None

shuffle 为 True 时使用。传入一个 int 以在多次函数调用中获得可重现的输出。请参阅 词汇表

error_score‘raise’ or numeric, default=np.nan

如果估计器拟合中发生错误,分配给分数的值。如果设置为 'raise',则会引发错误。如果给定一个数值,则会引发 FitFailedWarning。

fit_paramsdict, default=None

要传递给估计器拟合方法的参数。

axmatplotlib Axes, default=None

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

negate_scorebool, default=False

是否对通过 learning_curve 获得的分数取反。这在使用 scikit-learn 中表示为 neg_* 的误差时特别有用。

score_namestr, default=None

用于修饰绘图 y 轴的分数名称。它将覆盖从 scoring 参数推断出的名称。如果 scoreNone,则如果 negate_scoreFalse,我们使用 "Score",否则使用 "Negative score"。如果 scoring 是字符串或可调用对象,我们推断名称。我们将 _ 替换为空格并大写首字母。如果 negate_scoreFalse,我们删除 neg_ 并替换为 "Negative",否则直接删除它。

score_type{“test”, “train”, “both”}, default=”both”

要绘制的分数类型。可以是 "test""train""both" 之一。

std_display_style{“errorbar”, “fill_between”} or None, default=”fill_between”

用于显示平均分数周围分数标准差的样式。如果为 None,则不显示标准差的表示。

line_kwdict, 默认=None

传递给用于绘制平均分数的 plt.plot 的附加关键字参数。

fill_between_kwdict, default=None

传递给用于绘制分数标准差的 plt.fill_between 的附加关键字参数。

errorbar_kwdict, default=None

传递给用于绘制平均分数和标准差分数的 plt.errorbar 的附加关键字参数。

返回:
displayLearningCurveDisplay

存储计算值的对象。

示例

>>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import LearningCurveDisplay
>>> from sklearn.tree import DecisionTreeClassifier
>>> X, y = load_iris(return_X_y=True)
>>> tree = DecisionTreeClassifier(random_state=0)
>>> LearningCurveDisplay.from_estimator(tree, X, y)
<...>
>>> plt.show()
../../_images/sklearn-model_selection-LearningCurveDisplay-2.png
plot(ax=None, *, negate_score=False, score_name=None, score_type='both', std_display_style='fill_between', line_kw=None, fill_between_kw=None, errorbar_kw=None)[source]#

绘制可视化图。

参数:
axmatplotlib Axes, default=None

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

negate_scorebool, default=False

是否对通过 learning_curve 获得的分数取反。这在使用 scikit-learn 中表示为 neg_* 的误差时特别有用。

score_namestr, default=None

用于修饰绘图 y 轴的分数名称。它将覆盖从 scoring 参数推断出的名称。如果 scoreNone,则如果 negate_scoreFalse,我们使用 "Score",否则使用 "Negative score"。如果 scoring 是字符串或可调用对象,我们推断名称。我们将 _ 替换为空格并大写首字母。如果 negate_scoreFalse,我们删除 neg_ 并替换为 "Negative",否则直接删除它。

score_type{“test”, “train”, “both”}, default=”both”

要绘制的分数类型。可以是 "test""train""both" 之一。

std_display_style{“errorbar”, “fill_between”} or None, default=”fill_between”

用于显示平均分数周围分数标准差的样式。如果为 None,则不显示标准差表示。

line_kwdict, 默认=None

传递给用于绘制平均分数的 plt.plot 的附加关键字参数。

fill_between_kwdict, default=None

传递给用于绘制分数标准差的 plt.fill_between 的附加关键字参数。

errorbar_kwdict, default=None

传递给用于绘制平均分数和标准差分数的 plt.errorbar 的附加关键字参数。

返回:
displayLearningCurveDisplay

存储计算值的对象。