【问题标题】:Change y labels in a python sklearn partial dependence plot [closed]在python sklearn部分依赖图中更改y标签[关闭]
【发布时间】:2021-07-16 18:19:21
【问题描述】:

我想将 ylabel 从部分依赖图从“部分依赖”更改为“失败概率”。

这篇文章类似于change x labels in a python sklearn partial dependence plot,但解决方案不起作用,显然 y_axis 是硬编码在函数中的 (line 740 of the current partial_dependence.py source code)。

【问题讨论】:

  • 请将相关代码发布在这里,不要在外部repos中;见How to Ask

标签: python matplotlib machine-learning scikit-learn


【解决方案1】:

绘图共享 y 轴,因此在轴上调用 set_ylabel 可能无法设置正确的轴。

解决方法如下:

fig, ax = plt.subplots(1, 1)
pdp = plot_partial_dependence(
    clf, X_train, features, feature_names=names, n_jobs=3, ax=ax, grid_resolution=50
)
pdp.axes_[0][0].set_ylabel("Failure Probability")

完整代码:

from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.inspection import plot_partial_dependence
from sklearn.datasets import fetch_california_housing
import matplotlib.pyplot as plt

cal_housing = fetch_california_housing()

X_train, X_test, y_train, y_test = train_test_split(
    cal_housing.data, cal_housing.target, test_size=0.2, random_state=1
)
names = cal_housing.feature_names
clf = GradientBoostingRegressor(
    n_estimators=100, max_depth=4, learning_rate=0.1, loss="huber", random_state=1
)
clf.fit(X_train, y_train)

features = [0, 5, 1]
fig, ax = plt.subplots(1, 1)
pdp = plot_partial_dependence(
    clf, X_train, features, feature_names=names, n_jobs=3, ax=ax, grid_resolution=50
)
pdp.axes_[0][0].set_ylabel("Failure Probability")

fig.suptitle(
    "Partial dependence of house value on nonlocation features\n"
    "for the California housing dataset"
)

plt.show()

结果:

【讨论】:

  • 太棒了!非常感谢。
猜你喜欢
  • 2018-05-03
  • 2017-03-22
  • 2021-11-09
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多