注意
转到末尾以下载完整示例代码,或通过 JupyterLite 或 Binder 在浏览器中运行此示例。
使用偏依赖进行高级绘图#
PartialDependenceDisplay 对象可用于绘图,而无需重新计算偏依赖。在此示例中,我们展示了如何绘制偏依赖图,以及如何使用可视化 API 快速自定义绘图。
注意
另请参阅 使用可视化 API 绘制 ROC 曲线
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.datasets import load_diabetes
from sklearn.inspection import PartialDependenceDisplay
from sklearn.neural_network import MLPRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeRegressor
在糖尿病数据集上训练模型#
首先,我们在糖尿病数据集上训练一个决策树和一个多层感知器。
diabetes = load_diabetes()
X = pd.DataFrame(diabetes.data, columns=diabetes.feature_names)
y = diabetes.target
tree = DecisionTreeRegressor()
mlp = make_pipeline(
StandardScaler(),
MLPRegressor(hidden_layer_sizes=(100, 100), tol=1e-2, max_iter=500, random_state=0),
)
tree.fit(X, y)
mlp.fit(X, y)
绘制两个特征的偏依赖#
我们绘制决策树的特征“age”和“bmi”(身体质量指数)的偏依赖曲线。对于两个特征,from_estimator 预期会绘制两条曲线。在这里,绘图函数使用 ax 定义的空间放置一个包含两个图的网格。
fig, ax = plt.subplots(figsize=(12, 6))
ax.set_title("Decision Tree")
tree_disp = PartialDependenceDisplay.from_estimator(tree, X, ["age", "bmi"], ax=ax)

可以绘制多层感知器的偏依赖曲线。在这种情况下,将 line_kw 传递给 from_estimator 以更改曲线的颜色。
fig, ax = plt.subplots(figsize=(12, 6))
ax.set_title("Multi-layer Perceptron")
mlp_disp = PartialDependenceDisplay.from_estimator(
mlp, X, ["age", "bmi"], ax=ax, line_kw={"color": "red"}
)

将两个模型的偏依赖一起绘制#
tree_disp 和 mlp_disp PartialDependenceDisplay 对象包含重新创建偏依赖曲线所需的所有计算信息。这意味着我们可以轻松创建额外的图表,而无需重新计算曲线。
一种绘制曲线的方法是将它们放在同一张图中,每个模型的曲线占据一行。首先,我们创建一个包含两行一列的图。将这两个轴传递给 tree_disp 和 mlp_disp 的 plot 函数。绘图函数将使用给定的轴来绘制偏依赖。生成的图将决策树偏依赖曲线放在第一行,多层感知器放在第二行。
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10))
tree_disp.plot(ax=ax1)
ax1.set_title("Decision Tree")
mlp_disp.plot(ax=ax2, line_kw={"color": "red"})
ax2.set_title("Multi-layer Perceptron")

Text(0.5, 1.0, 'Multi-layer Perceptron')
另一种比较曲线的方法是将它们叠加绘制。在这里,我们创建一个包含一行两列的图。将轴作为列表传递给 plot 函数,它将在相同的轴上绘制每个模型的偏依赖曲线。轴列表的长度必须等于绘制的图数。
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))
tree_disp.plot(ax=[ax1, ax2], line_kw={"label": "Decision Tree"})
mlp_disp.plot(
ax=[ax1, ax2], line_kw={"label": "Multi-layer Perceptron", "color": "red"}
)
ax1.legend()
ax2.legend()

<matplotlib.legend.Legend object at 0x7fb4a21a56d0>
tree_disp.axes_ 是一个 numpy 数组容器,包含用于绘制偏依赖图的轴。可以将其传递给 mlp_disp 以实现将图叠加绘制的相同效果。此外,mlp_disp.figure_ 存储了图,允许在调用 plot 之后调整图的大小。在这种情况下,tree_disp.axes_ 有两个维度,因此 plot 只会在最左侧的图上显示 y 标签和 y 刻度。
tree_disp.plot(line_kw={"label": "Decision Tree"})
mlp_disp.plot(
line_kw={"label": "Multi-layer Perceptron", "color": "red"}, ax=tree_disp.axes_
)
tree_disp.figure_.set_size_inches(10, 6)
tree_disp.axes_[0, 0].legend()
tree_disp.axes_[0, 1].legend()
plt.show()

绘制单个特征的偏依赖#
在这里,我们在相同的轴上绘制单个特征“age”的偏依赖曲线。在这种情况下,将 tree_disp.axes_ 传递给第二个绘图函数。
tree_disp = PartialDependenceDisplay.from_estimator(tree, X, ["age"])
mlp_disp = PartialDependenceDisplay.from_estimator(
mlp, X, ["age"], ax=tree_disp.axes_, line_kw={"color": "red"}
)

脚本总运行时间: (0 minutes 2.296 seconds)
相关示例