在 python sklearn 部分依赖图中更改 x 标签 [英] change x labels in a python sklearn partial dependence plot

查看:70
本文介绍了在 python sklearn 部分依赖图中更改 x 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi 使用归一化数据拟合 GradientBoostingRegressor 并绘制了主要 10 个变量的部分依赖关系.现在我想根据真实的非标准化值绘制它们,因此想要访问 x 标签.我该怎么做?

Hi used normalized data for fitting a GradientBoostingRegressor and plotted the partial dependecies for the main 10 variables. Now I want to plot them against the real, non-normalized values and therefore want to access the x labels. How do I do this?

我的代码相当于http://scikit-learn.org/stable/auto_examples/ensemble/plot_partial_dependence.html

对于 3D 绘图很容易,因为我可以转换轴

For the 3D plot its easy as I can convert the axes

axes[0] = (axes[0]*mysd0)+mymean0
axes[1] = (axes[1]*mysd1)+mymean1

具有平均值和标准偏差,但对于子图我不知道如何访问标签.谢谢

with the mean and standard deviation but for the subplots I dont know how to access the labels. Thx

这里是我正在谈论的代码部分:

Here the part of the code I am talking about:

from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble.partial_dependence import plot_partial_dependence
from sklearn.datasets.california_housing import fetch_california_housing

cal_housing = fetch_california_housing()

# split 80/20 train-test
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, axs = plot_partial_dependence(clf, X_train, features,
                                   feature_names=names,
                                   n_jobs=3, grid_resolution=50)
fig.suptitle('Partial dependence of house value on nonlocation features\n'
             'for the California housing dataset')

在这个图中,我想访问和操作 x 轴标签...

In this figure I want to access and manipulate the x axis labels...

推荐答案

我找到了解决方案,而且很明显... axs 包含所有轴信息作为列表.因此,它可以访问每个轴.因此,第一个子图的轴是 axs[0] 并获得标签:

I found the solution, and it was quite obvious... The axs contains all the axes information as a list. Therefore each axes can be accessed by it. The axes of the first subplot is therefore axs[0] and to get the labels its:

labels = [item.get_text() for item in axs[0].get_xticklabels()]

但是,在我的情况下,尽管图中显示了值,但它始终为空,这并不能用作标签.因此,我使用轴限制和以下代码来创建新的转换标签

however, this did not work as the labels in my case where always empty although values were displayed in the figure. I therefore used the axis limits and the following code to create the new transformed labels

    fig, axs = plot_partial_dependence(clf, X, features,feature_names=X.columns, grid_resolution=100)
    lims = plt.getp(axs[0],"xlim") 
    myxrange = np.linspace(lims[0],lims[1],5)                                  
    mymean = mean4bactransform
    mysd   = sd4bactransform
    newlabels = [str(round((myx*mysd)+mymean,2)) for myx in myxrange]           
    plt.setp(axs, xticks=myxrange, xticklabels=newlabels)                                               
    fig.suptitle('Partial dependence')
    plt.subplots_adjust(top=0.9)  # tight_layout causes overlap with suptitle
    fig.set_size_inches(10.5, 7.5)

这篇关于在 python sklearn 部分依赖图中更改 x 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆