Skip to content

Commit c170be1

Browse files
thomasjpfanadrinjalali
authored andcommitted
ENH Sets xlabel only if it is not defined in PDP (#15610)
1 parent cd84cb9 commit c170be1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

sklearn/inspection/_partial_dependence.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,9 +933,12 @@ def plot(self, ax=None, n_cols=3, line_kw=None, contour_kw=None):
933933
ylim = axi.get_ylim()
934934
axi.vlines(self.deciles[fx[0]], 0, 0.05, transform=trans,
935935
color='k')
936-
axi.set_xlabel(self.feature_names[fx[0]])
937936
axi.set_ylim(ylim)
938937

938+
# Set xlabel if it is not already set
939+
if not axi.get_xlabel():
940+
axi.set_xlabel(self.feature_names[fx[0]])
941+
939942
if len(values) == 1:
940943
if n_cols is None or i % n_cols == 0:
941944
axi.set_ylabel('Partial dependence')

sklearn/inspection/tests/test_plot_partial_dependence.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,24 @@ def test_plot_partial_dependence_with_same_axes(pyplot, clf_boston, boston):
256256
feature_names=boston.feature_names, ax=ax)
257257

258258

259+
def test_plot_partial_dependence_feature_name_reuse(pyplot, clf_boston,
260+
boston):
261+
# second call to plot does not change the feature names from the first
262+
# call
263+
264+
feature_names = boston.feature_names
265+
disp = plot_partial_dependence(clf_boston, boston.data,
266+
[0, 1],
267+
grid_resolution=10,
268+
feature_names=feature_names)
269+
270+
plot_partial_dependence(clf_boston, boston.data, [0, 1],
271+
grid_resolution=10, ax=disp.axes_)
272+
273+
for i, ax in enumerate(disp.axes_.ravel()):
274+
assert ax.get_xlabel() == feature_names[i]
275+
276+
259277
def test_plot_partial_dependence_multiclass(pyplot):
260278
grid_resolution = 25
261279
clf_int = GradientBoostingClassifier(n_estimators=10, random_state=1)

0 commit comments

Comments
 (0)