|
22 | 22 | from sklearn.datasets import make_classification, make_regression
|
23 | 23 | from sklearn.cluster import KMeans
|
24 | 24 | from sklearn.metrics import r2_score
|
| 25 | +from sklearn.pipeline import make_pipeline |
25 | 26 | from sklearn.preprocessing import PolynomialFeatures
|
| 27 | +from sklearn.preprocessing import StandardScaler |
26 | 28 | from sklearn.dummy import DummyClassifier
|
27 | 29 | from sklearn.base import BaseEstimator, ClassifierMixin
|
28 | 30 | from sklearn.utils.testing import assert_allclose
|
@@ -393,6 +395,31 @@ def test_partial_dependence_sample_weight():
|
393 | 395 | assert np.corrcoef(pdp, values)[0, 1] > 0.99
|
394 | 396 |
|
395 | 397 |
|
| 398 | +def test_partial_dependence_pipeline(): |
| 399 | + # check that the partial dependence support pipeline |
| 400 | + iris = load_iris() |
| 401 | + |
| 402 | + scaler = StandardScaler() |
| 403 | + clf = DummyClassifier(random_state=42) |
| 404 | + pipe = make_pipeline(scaler, clf) |
| 405 | + |
| 406 | + clf.fit(scaler.fit_transform(iris.data), iris.target) |
| 407 | + pipe.fit(iris.data, iris.target) |
| 408 | + |
| 409 | + features = 0 |
| 410 | + pdp_pipe, values_pipe = partial_dependence( |
| 411 | + pipe, iris.data, features=[features] |
| 412 | + ) |
| 413 | + pdp_clf, values_clf = partial_dependence( |
| 414 | + clf, scaler.transform(iris.data), features=[features] |
| 415 | + ) |
| 416 | + assert_allclose(pdp_pipe, pdp_clf) |
| 417 | + assert_allclose( |
| 418 | + values_pipe[0], |
| 419 | + values_clf[0] * scaler.scale_[features] + scaler.mean_[features] |
| 420 | + ) |
| 421 | + |
| 422 | + |
396 | 423 | def test_plot_partial_dependence(pyplot):
|
397 | 424 | # Test partial dependence plot function.
|
398 | 425 | boston = load_boston()
|
|
0 commit comments