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_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_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()
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)
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) meta.fit(X_dsel, y_dsel)