梯度提升的单调约束

Machine LearningMachine LearningBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

这是一个循序渐进的教程,用于演示单调约束对梯度提升估计器的影响。梯度提升是一种用于回归和分类任务的流行机器学习技术。在本教程中,我们将构建一个人工数据集,并使用梯度提升估计器来演示单调约束对模型预测的影响。

虚拟机提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,请随时向 Labby 提问。课程结束后提供反馈,我们将立即为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) sklearn(("Sklearn")) -.-> sklearn/ModelSelectionandEvaluationGroup(["Model Selection and Evaluation"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/ensemble("Ensemble Methods") sklearn/ModelSelectionandEvaluationGroup -.-> sklearn/inspection("Inspection") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/ensemble -.-> lab-49218{{"梯度提升的单调约束"}} sklearn/inspection -.-> lab-49218{{"梯度提升的单调约束"}} ml/sklearn -.-> lab-49218{{"梯度提升的单调约束"}} end

导入库

我们将首先导入本教程所需的必要库。

from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.inspection import PartialDependenceDisplay
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

生成数据

我们将生成一个人工数据集,其中目标值与第一个特征正相关,与第二个特征负相关。我们还将添加一些随机噪声,以使数据更逼真。

rng = np.random.RandomState(0)

n_samples = 1000
f_0 = rng.rand(n_samples)
f_1 = rng.rand(n_samples)
X = np.c_[f_0, f_1]
noise = rng.normal(loc=0.0, scale=0.01, size=n_samples)

y = 5 * f_0 + np.sin(10 * np.pi * f_0) - 5 * f_1 - np.cos(10 * np.pi * f_1) + noise

拟合无约束模型

我们将在生成的数据上拟合一个无任何约束的模型,以观察该模型在没有任何限制的情况下的表现。

gbdt_no_cst = HistGradientBoostingRegressor()
gbdt_no_cst.fit(X, y)

拟合具有单调约束的模型

现在我们将在相同的数据上拟合另一个模型,但对特征施加单调约束。我们将对第一个特征施加单调递增约束,对第二个特征施加单调递减约束。

gbdt_with_monotonic_cst = HistGradientBoostingRegressor(monotonic_cst=[1, -1])
gbdt_with_monotonic_cst.fit(X, y)

显示偏依赖关系

现在我们将展示两个模型的预测对两个特征的偏依赖关系。

fig, ax = plt.subplots()
disp = PartialDependenceDisplay.from_estimator(
    gbdt_no_cst,
    X,
    features=[0, 1],
    feature_names=(
        "First feature",
        "Second feature",
    ),
    line_kw={"linewidth": 4, "label": "unconstrained", "color": "tab:blue"},
    ax=ax,
)
PartialDependenceDisplay.from_estimator(
    gbdt_with_monotonic_cst,
    X,
    features=[0, 1],
    line_kw={"linewidth": 4, "label": "constrained", "color": "tab:orange"},
    ax=disp.axes_,
)

for f_idx in (0, 1):
    disp.axes_[0, f_idx].plot(
        X[:, f_idx], y, "o", alpha=0.3, zorder=-1, color="tab:green"
    )
    disp.axes_[0, f_idx].set_ylim(-6, 6)

plt.legend()
fig.suptitle("Monotonic constraints effect on partial dependences")
plt.show()

使用特征名称指定单调约束

如果训练数据有特征名称,则可以通过传递字典来指定单调约束。现在我们将使用相同的数据并通过特征名称指定约束来演示这一点。

X_df = pd.DataFrame(X, columns=["f_0", "f_1"])

gbdt_with_monotonic_cst_df = HistGradientBoostingRegressor(
    monotonic_cst={"f_0": 1, "f_1": -1}
).fit(X_df, y)

np.allclose(
    gbdt_with_monotonic_cst_df.predict(X_df), gbdt_with_monotonic_cst.predict(X)
)

总结

在本教程中,我们展示了单调约束对梯度提升估计器的影响。我们生成了一个人工数据集,拟合了两个模型,一个没有任何约束,另一个有单调约束,并展示了预测对两个特征的偏依赖关系。我们还演示了如何使用特征名称来指定单调约束。