def test_label_encoder():
    y = ['one', 'one', 'one', 'zero', 'zero', 'two']
    X = np.random.rand(6, 3)
    pool = [DecisionTreeClassifier().fit(X, y) for _ in range(5)]
    stacked = StackedClassifier(pool).fit(X, y)
    pred = stacked.predict(X)
    assert np.array_equal(pred, y)
def test_one_class_meta_dataset(create_X_y):
    X, y = create_X_y
    pool = [DecisionTreeClassifier().fit(X, y) for _ in range(5)]
    stacked = StackedClassifier(pool)
    X_meta = np.random.rand(10, 2)
    y_meta = np.zeros(10, dtype=int)
    with pytest.raises(ValueError):
        stacked.fit(X_meta, y_meta)
def test_label_encoder_base_ensemble():
    from sklearn.ensemble import RandomForestClassifier
    X, y = make_classification()
    y[y == 1] = 2
    y = y.astype(np.float)
    pool = RandomForestClassifier().fit(X, y)
    st = StackedClassifier(pool)
    st.fit(X, y)
    pred = st.predict(X)
    assert np.isin(st.classes_, pred).all()
Exemple #4
0
def test_not_predict_proba(create_X_y):
    X, y = create_X_y

    clf1 = Perceptron()
    clf1.fit(X, y)
    with pytest.raises(ValueError):
        StackedClassifier([clf1, clf1]).fit(X, y)
Exemple #5
0
def test_not_predict_proba_meta(create_X_y, create_pool_classifiers):
    X, y = create_X_y

    pool = create_pool_classifiers
    with pytest.raises(ValueError):
        meta_clf = StackedClassifier(pool_classifiers=pool,
                                     meta_classifier=Perceptron())
        meta_clf.fit(X, y)
        meta_clf.predict_proba(X)
def test_single_model_pool(create_X_y):
    X, y = create_X_y
    pool = [DecisionTreeClassifier().fit(X, y)]
    with pytest.raises(ValueError):
        StackedClassifier(pool_classifiers=pool).fit(X, y)
def test_passthrough_false(create_X_y):
    X, y = create_X_y
    pool = [DecisionTreeClassifier().fit(X, y) for _ in range(5)]
    stacked = StackedClassifier(pool, passthrough=False)
    stacked.fit(X, y)
    assert stacked.meta_classifier_.coef_.shape == (1, 5)
Exemple #8
0
def test_check_estimator():
    check_estimator(StackedClassifier())
Exemple #9
0
                                                    random_state=rng)
RF = RandomForestClassifier(random_state=rng)
RF.fit(X_train, y_train)

X_train, X_dsel, y_train, y_dsel = train_test_split(X_train, y_train,
                                                    test_size=0.50,
                                                    random_state=rng)

# Training a random forest to be used as the pool of classifiers.
# We set the maximum depth of the tree so that it
# can estimate probabilities
pool_classifiers = RandomForestClassifier(n_estimators=100, max_depth=5,
                                          random_state=rng)
pool_classifiers.fit(X_train, y_train)

stacked = StackedClassifier(pool_classifiers, LogisticRegression())
stacked.fit(X_dsel, y_dsel)

# Initialize a DS technique. Here we specify the size of
# the region of competence (5 neighbors)
knorau = KNORAU(pool_classifiers, random_state=rng)
kne = KNORAE(pool_classifiers, k=5, random_state=rng)
desp = DESP(pool_classifiers, k=5, random_state=rng)
ola = OLA(pool_classifiers, k=5, random_state=rng)
mcb = MCB(pool_classifiers, k=5, random_state=rng)
meta = METADES(pool_classifiers, k=5, random_state=rng)

# Fit the DS techniques
knorau.fit(X_dsel, y_dsel)
kne.fit(X_dsel, y_dsel)
desp.fit(X_dsel, y_dsel)