【问题标题】:change x labels in a python sklearn partial dependence plot在 python sklearn 部分依赖图中更改 x 标签
【发布时间】:2018-05-03 23:40:27
【问题描述】:

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

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

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

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

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

这里是我说的部分代码:

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 轴标签...

【问题讨论】:

  • 你能描述一下 x 矩阵吗?列 ?行?还要添加完整的代码

标签: python scikit-learn sklearn-pandas


【解决方案1】:

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

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

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

    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)

【讨论】:

    【解决方案2】:

    如果我理解正确您想根据特征重要性访问标签

    如果是这种情况,那么您可以执行以下操作:

    #after fitting the model use this to get the feature importance
    feature_importance = clf.feature_importances_
    
    # make importances relative to max importance
    feature_importance = 100.0 * (feature_importance / feature_importance.max())
    
    # sort the importances and get the indices of the sorting
    sorted_idx = np.argsort(feature_importance)
    
    #match the indices with the labels of the x matrix
    #important: x must have columns names to do this
    x.columns[feature_names[sorted_idx]]
    

    这将按升序为您提供功能名称。这意味着名字是最不重要的特征,而姓氏是最重要的特征。

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 2021-11-09
      • 2017-03-22
      • 2014-08-16
      • 2018-02-06
      • 2015-07-20
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多