Exemplo n.º 1
0
def test_gp_search():
    # Test that the best estimator contains the right value for foo_param
    clf = MockDiscreteClassifier()
    gp_search = GPSearchCV(clf, {'foo_param': ['int', [1, 3]]})
    # make sure it selects the smallest parameter in case of ties
    old_stdout = sys.stdout
    sys.stdout = StringIO()
    gp_search.fit(X, y)
    sys.stdout = old_stdout
    assert_equal(gp_search.best_estimator_.foo_param, 2)

    clf = MockContinuousClassifier()
    gp_search = GPSearchCV(clf, {'foo_param': ['float', [-3, 3]]}, verbose=3)
    # make sure it selects the smallest parameter in case of ties
    old_stdout = sys.stdout
    sys.stdout = StringIO()
    gp_search.fit(X, y)
    sys.stdout = old_stdout
    assert_almost_equal(gp_search.best_estimator_.foo_param, 0, decimal=1)

    # Smoke test the score etc:
    gp_search.score(X, y)
    gp_search.predict_proba(X)
    gp_search.decision_function(X)
    gp_search.transform(X)

    # Test exception handling on scoring
    gp_search.scoring = 'sklearn'
    assert_raises(ValueError, gp_search.fit, X, y)
Exemplo n.º 2
0
def test_gp_search():
    clf = MockClassifier()
    gp_search = GPSearchCV(clf, {'foo_param': ['int', [1, 3]]}, verbose=3)
    # make sure it selects the smallest parameter in case of ties
    old_stdout = sys.stdout
    sys.stdout = StringIO()
    gp_search.fit(X, y)
    sys.stdout = old_stdout
    assert_equal(gp_search.best_estimator_.foo_param, 2)

    for i, foo_i in enumerate([1, 2, 3]):
        assert_true(gp_search.scores_[i][0]
                    == {'foo_param': foo_i})
    # Smoke test the score etc:
    gp_search.score(X, y)
    gp_search.predict_proba(X)
    gp_search.decision_function(X)
    gp_search.transform(X)

    # Test exception handling on scoring
    gp_search.scoring = 'sklearn'
    assert_raises(ValueError, gp_search.fit, X, y)
Exemplo n.º 3
0
def test_grid_search_no_score():
    # Test grid-search on classifier that has no score function.
    clf = LinearSVC(random_state=0)
    X, y = make_blobs(random_state=0, centers=2)
    Cs = [.1, 1, 10]
    clf_no_score = LinearSVCNoScore(random_state=0)
    gp_search = GPSearchCV(clf, {'C': Cs}, scoring='accuracy')
    gp_search.fit(X, y)

    grid_search_no_score = GPSearchCV(clf_no_score, {'C': Cs},
                                      scoring='accuracy')
    # smoketest grid search
    grid_search_no_score.fit(X, y)

    # check that best params are equal
    assert_equal(grid_search_no_score.best_params_, gp_search.best_params_)
    # check that we can call score and that it gives the correct result
    assert_equal(gp_search.score(X, y), grid_search_no_score.score(X, y))

    # giving no scoring function raises an error
    grid_search_no_score = GPSearchCV(clf_no_score, {'C': Cs})
    assert_raise_message(TypeError, "no scoring", grid_search_no_score.fit,
                         [[1]])