注意:单击此处 https://urlify.cn/BNjeIj 下载完整的示例代码,或通过Binder在浏览器中运行此示例
plot_partial_dependence
函数返回一个可用于绘图的
PartialDependenceDisplay
对象,而无需重新计算部分依赖。在此示例中,我们将展示如何绘制部分依赖图(partial dependence plots)以及如何使用可视化API来快速自定义图。
注意: 另请参阅 用可视化API绘制ROC曲线
print(__doc__)
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.neural_network import MLPRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.tree import DecisionTreeRegressor
from sklearn.inspection import plot_partial_dependence
在波士顿房屋价格数据集上训练模型
首先,我们在波士顿房价数据集上训练决策树和多层感知器。boston = load_boston()
X = pd.DataFrame(boston.data, columns=boston.feature_names)
y = boston.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)
输出:
Pipeline(steps=[('standardscaler', StandardScaler()),
('mlpregressor',
MLPRegressor(hidden_layer_sizes&#