Ejemplo n.º 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) )
Ejemplo n.º 2
0
 def test_parameters(self):
     iris = load_iris()
     X, y = iris.data, iris.target
     estimator = MultinomialNB(alpha=2.0, fit_prior=False)
     estimator.fit(X, y)  
     
     fname = tempfile.NamedTemporaryFile().name
     f = EstimatorStore(fname, "w")
     path = "nb"
     f.store_fit(estimator, path, set_params=True)
     f.close()
     
     estimator = MultinomialNB()
     f = EstimatorStore(fname)
     f.restore_fit(estimator, path, set_params=True)
     
     assert estimator.alpha == 2.0
     assert estimator.fit_prior is False
Ejemplo n.º 3
0
 def test_single_estimator(self):
     iris = load_iris()
     X, y = iris.data, iris.target
     estimator = MultinomialNB()
     estimator.fit(X, y)  
     class_priors = estimator.class_log_prior_ 
     feat_prob = estimator.feature_log_prob_
     
     fname = tempfile.NamedTemporaryFile().name
     f = EstimatorStore(fname, "w")
     path = "nb"
     f.store_fit(estimator, path)
     f.close()
     
     estimator = MultinomialNB()
     f = EstimatorStore(fname)
     f.restore_fit(estimator, path)
     
     assert_array_equal(class_priors, estimator.class_log_prior_ )
     assert_array_equal(feat_prob, estimator.feature_log_prob_)