更改python sklearn部分依赖图中的y标签

2024-04-29 09:07:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将部分依赖图中的ylabel从“部分依赖”更改为“失效概率”

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


Tags: ofthe函数inlabelsplotlinesklearn
1条回答
网友
1楼 · 发布于 2024-04-29 09:07:40

这些图共享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()

结果:

Output image, showing the y-axis is labeled 'Failure Probability'

相关问题 更多 >