Search code examples
pythonscikit-learnsklearn-pandas

change x labels in a python sklearn partial dependence plot


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?

My code is comparable to http://scikit-learn.org/stable/auto_examples/ensemble/plot_partial_dependence.html

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')

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


Solution

  • 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)