Ejemplo n.º 1
0
def test_bagging():
    X, y = load_iris(return_X_y=True)
    X_ = X.tolist()
    for y_ in [y, (y == 0).astype(int), (y == 2).astype(int)]:
        for n_estimators in [1, 10]:
            for max_depth in [5, 10, None]:
                for max_features in [0.75, 1.0]:
                    dt = DecisionTreeClassifier(max_depth=max_depth,
                                                random_state=5)
                    clf = BaggingClassifier(
                        dt,
                        bootstrap=False,
                        n_estimators=n_estimators,
                        random_state=5,
                        max_features=max_features,
                    )
                    clf.fit(X, y_)
                    clf_ = convert_estimator(clf)

                    for method in METHODS:
                        with warnings.catch_warnings():
                            warnings.simplefilter("ignore")
                            scores = getattr(clf, method)(X)
                        scores_ = getattr(clf_, method)(X_)
                        assert np.allclose(scores.shape, shape(scores_))
                    assert np.allclose(scores, scores_, equal_nan=True)
Ejemplo n.º 2
0
def test_complement():
    X, y = load_iris(return_X_y=True)
    X_ = X.tolist()
    for y_ in [y, (y == 0).astype(int), (y == 2).astype(int)]:
        clf = ComplementNB()
        clf.fit(X, y_)
        clf_ = convert_estimator(clf)

        for method in METHODS:
            scores = getattr(clf, method)(X)
            scores_ = getattr(clf_, method)(X_)
            assert np.allclose(scores.shape, shape(scores_))
            assert np.allclose(scores, scores_, equal_nan=True)
Ejemplo n.º 3
0
def test_dummy():
    X, y = load_iris(return_X_y=True)
    X_ = X.tolist()
    for y_ in [y, (y == 0).astype(int), (y == 2).astype(int)]:
        clf = DummyClassifier(strategy="prior")
        clf.fit(X, y_)
        clf_ = convert_estimator(clf)

        for method in METHODS:
            scores = getattr(clf, method)(X)
            scores_ = getattr(clf_, method)(X_)
            assert np.allclose(scores.shape, shape(scores_))
            assert np.allclose(scores, scores_, equal_nan=True)
Ejemplo n.º 4
0
def test_ridge():
    X, y = load_iris(return_X_y=True)
    X_ = X.tolist()
    for y_ in [y, (y == 0).astype(int), (y == 2).astype(int)]:
        for fit_intercept in [True, False]:
            clf = RidgeClassifier(fit_intercept=fit_intercept)
            clf.fit(X, y_)
            clf_ = convert_estimator(clf)

            for method in METHODS:
                scores = getattr(clf, method)(X)
                scores_ = getattr(clf_, method)(X_)
                assert np.allclose(scores.shape, shape(scores_))
                assert np.allclose(scores, scores_)
Ejemplo n.º 5
0
def test_extra_tree_reg():
    X, y = load_iris(return_X_y=True)
    X_ = X.tolist()
    for y_ in [(y == 0).astype(int), (y == 2).astype(int)]:
        for max_depth in [5, 10, None]:
            clf = ExtraTreeRegressor(max_depth=max_depth, random_state=5)
            clf.fit(X, y_)
            clf_ = convert_estimator(clf)

            for method in ["predict"]:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    scores = getattr(clf, method)(X)
                scores_ = getattr(clf_, method)(X_)
                assert np.allclose(scores.shape, shape(scores_))
                assert np.allclose(scores, scores_, equal_nan=True)
Ejemplo n.º 6
0
def test_extra_tree_clf():
    X, y = load_iris(return_X_y=True)
    X_ = X.tolist()
    for y_ in [y, (y == 0).astype(int), (y == 2).astype(int)]:
        for max_depth in [5, 10, None]:
            clf = ExtraTreeClassifier()
            clf.fit(X, y_)
            clf_ = convert_estimator(clf)

            for method in METHODS:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    scores = getattr(clf, method)(X)
                scores_ = getattr(clf_, method)(X_)
                assert np.allclose(scores.shape, shape(scores_))
                assert np.allclose(scores, scores_, equal_nan=True)
Ejemplo n.º 7
0
def test_missing_indicator():
    X, y = load_iris(return_X_y=True)
    for missing_values in [np.nan, X[0][0], X[-1][1]]:
        X, y = load_iris(return_X_y=True)
        if np.isnan(missing_values):
            X.ravel()[np.random.choice(X.size, 20, replace=False)] = np.nan
        X_ = X.tolist()
        for features in ["missing-only", "all"]:
            imp = MissingIndicator(
                features=features, missing_values=missing_values, error_on_new=False
            )
            imp.fit(X)
            imp_ = convert_estimator(imp)

            X_t = getattr(imp, "transform")(X)
            X_t_ = getattr(imp_, "transform")(X_)
            assert np.allclose(X_t.shape, shape(X_t_))
            assert np.allclose(X_t, X_t_)
Ejemplo n.º 8
0
def test_sgd():
    X, y = load_iris(return_X_y=True)
    X_ = X.tolist()
    for y_ in [y, (y == 0).astype(int), (y == 2).astype(int)]:
        for loss in LOSSES:
            for fit_intercept in [True, False]:
                clf = SGDClassifier(fit_intercept=fit_intercept,
                                    max_iter=MAX_ITER,
                                    tol=TOL,
                                    loss=loss)
                clf.fit(X, y_)
                clf_ = convert_estimator(clf)

                for method in METHODS:
                    if hasattr(clf, method) and hasattr(clf_, method):
                        with warnings.catch_warnings():
                            warnings.simplefilter("ignore")
                            scores = getattr(clf, method)(X)
                        scores_ = getattr(clf_, method)(X_)
                        assert np.allclose(scores.shape, shape(scores_))
                        assert np.allclose(scores, scores_, equal_nan=True)
Ejemplo n.º 9
0
def test_simple_imputer():
    X, y = load_iris(return_X_y=True)
    for missing_values in [np.nan, X[0][0], X[-1][1]]:
        X, y = load_iris(return_X_y=True)
        if np.isnan(missing_values):
            X.ravel()[np.random.choice(X.size, 20, replace=False)] = np.nan
        X_ = X.tolist()
        for strategy in ["mean", "median", "most_frequent", "constant"]:
            for add_indicator in [True, False]:
                imp = SimpleImputer(strategy=strategy,
                                    missing_values=missing_values)
                if hasattr(imp, "add_indicator"):
                    imp.add_indicator = add_indicator
                else:
                    imp.add_indicator = False
                imp.fit(X)
                imp_ = convert_estimator(imp)

                X_t = getattr(imp, "transform")(X)
                X_t_ = getattr(imp_, "transform")(X_)
                assert np.allclose(X_t.shape, shape(X_t_))
                assert np.allclose(X_t, X_t_)
Ejemplo n.º 10
0
def test_transpose():
    assert transpose(B) == np.array(B).T.tolist()
    assert shape(transpose(B)) == shape(B)[::-1]