|
18 | 18 | from sklearn.utils.testing import assert_true
|
19 | 19 | from sklearn.utils.testing import assert_array_equal
|
20 | 20 | from sklearn.utils.testing import assert_array_almost_equal
|
21 |
| -from sklearn.utils.testing import assert_dict_equal |
22 | 21 |
|
23 | 22 | from sklearn.base import clone, BaseEstimator
|
24 | 23 | from sklearn.pipeline import Pipeline, FeatureUnion, make_pipeline, make_union
|
|
32 | 31 | from sklearn.preprocessing import StandardScaler
|
33 | 32 | from sklearn.feature_extraction.text import CountVectorizer
|
34 | 33 | from sklearn.externals.joblib import Memory
|
| 34 | +from sklearn.externals.joblib import hash |
| 35 | +from sklearn.utils.validation import check_is_fitted |
35 | 36 |
|
36 | 37 |
|
37 | 38 | JUNK_FOOD_DOCS = (
|
@@ -537,17 +538,22 @@ def make():
|
537 | 538 | assert_array_equal([[exp]], pipeline.fit_transform(X, y))
|
538 | 539 | assert_array_equal([exp], pipeline.fit(X).predict(X))
|
539 | 540 | assert_array_equal(X, pipeline.inverse_transform([[exp]]))
|
540 |
| - print(pipeline.get_params(deep=True)) |
541 |
| - assert_dict_equal(pipeline.get_params(deep=True), |
542 |
| - {'last': Mult(mult=5), |
543 |
| - 'memory': None, |
544 |
| - 'last__mult': 5, |
545 |
| - 'steps': [('m2', Mult(mult=2)), |
546 |
| - ('m3', None), |
547 |
| - ('last', Mult(mult=5))], |
548 |
| - 'm2__mult': 2, |
549 |
| - 'm3': None, |
550 |
| - 'm2': Mult(mult=2)}) |
| 541 | + |
| 542 | + pipeline_params = pipeline.get_params(deep=True) |
| 543 | + pipeline_params2 = {'steps': pipeline.steps, |
| 544 | + 'm2': mult2, |
| 545 | + 'm3': None, |
| 546 | + 'last': mult5, |
| 547 | + 'memory': None, |
| 548 | + 'm2__mult': 2, |
| 549 | + 'last__mult': 5} |
| 550 | + # check if the keys are the same |
| 551 | + assert_equal(sorted(pipeline_params.keys()), |
| 552 | + sorted(pipeline_params2.keys())) |
| 553 | + # check if the arrays are the same using joblib.hash |
| 554 | + for k in pipeline_params.keys(): |
| 555 | + assert_equal(hash(pipeline_params[k]), |
| 556 | + hash(pipeline_params2[k])) |
551 | 557 |
|
552 | 558 | pipeline.set_params(m2=None)
|
553 | 559 | exp = 5
|
@@ -621,6 +627,22 @@ def test_pipeline_ducktyping():
|
621 | 627 | assert_false(hasattr(pipeline, 'inverse_transform'))
|
622 | 628 |
|
623 | 629 |
|
| 630 | +def test_pipeline_steps(): |
| 631 | + iris = load_iris() |
| 632 | + X = iris.data |
| 633 | + y = iris.target |
| 634 | + clf = SVC(probability=True, random_state=0) |
| 635 | + pca = PCA(svd_solver='full', n_components='mle', whiten=True) |
| 636 | + pipe = Pipeline([('pca', pca), ('svc', clf)]) |
| 637 | + pipe.fit(X, y) |
| 638 | + |
| 639 | + # check that _steps was not change after fitting |
| 640 | + assert_equal(pca, pipe._steps[0][1]) |
| 641 | + assert_equal(clf, pipe._steps[1][1]) |
| 642 | + # check that the estimators have been fitted in steps_ |
| 643 | + check_is_fitted(pipe.named_steps_['pca'], 'n_components_') |
| 644 | + check_is_fitted(pipe.named_steps_['svc'], 'support_vectors_') |
| 645 | + |
624 | 646 | def test_make_pipeline():
|
625 | 647 | t1 = Transf()
|
626 | 648 | t2 = Transf()
|
|
0 commit comments