示例#1
0
    def test_pipeline(self):
        X, y = samples_generator.make_classification(
            n_informative=5, n_redundant=0, random_state=42)

        anova_filter = SelectKBest(f_regression, k=5)
        clf = SVC(kernel='linear')
        anova_svc = Pipeline([('anova', anova_filter), 
                              ('svc', clf)])
        anova_svc.fit(X, y)
        score = anova_svc.score(X,y)
        
        fname = tempfile.NamedTemporaryFile().name
        f = EstimatorStore(fname, "w")
        path = "anova_svc"
        f.store_fit(anova_svc, path, set_params=True)
        f.close()
        
        # redefine
        anova_filter2 = SelectKBest(f_regression)
        clf2 = SVC()
        anova_svc2 = Pipeline([('anova', anova_filter2), 
                               ('svc', clf2)])
            
        f = EstimatorStore(fname)
        f.restore_fit(anova_svc2, path, set_params=True)
        
        score2 = anova_svc2.score(X,y)
        
        assert score == score2
        
        for attr in f.get_fitted_attrs(anova_filter):
            assert_array_equal( getattr(anova_filter, attr),
                                getattr(anova_filter2, attr) )
            
        for attr in f.get_fitted_attrs(clf):
            assert_array_equal( getattr(clf, attr),
                                getattr(clf2, attr) )