def test_fit(): X = X_dsel_ex1 y = y_dsel_ex1 pool_classifiers = create_pool_classifiers() single_best_test = SingleBest(pool_classifiers) single_best_test.fit(X, y) assert single_best_test.best_clf_index == 0 or single_best_test.best_clf_index == 2
def test_not_predict_proba(): X = X_dsel_ex1 y = y_dsel_ex1 classifier = MagicMock() classifier.predict.return_value = [0] single_best_test = SingleBest([classifier] * 10) single_best_test.fit(X, y) with pytest.raises(ValueError): single_best_test.predict_proba(X)
def test_predict(): X = X_dsel_ex1 y = y_dsel_ex1 pool_classifiers = create_pool_classifiers() single_best_test = SingleBest(pool_classifiers) single_best_test.fit(X, y) predicted_labels = single_best_test.predict(X) assert np.equal(predicted_labels, 0).all()
def test_predict_proba(): X = X_dsel_ex1 y = y_dsel_ex1 pool_classifiers = create_pool_classifiers() single_best_test = SingleBest(pool_classifiers) single_best_test.fit(X, y) predicted_proba = single_best_test.predict_proba(X) assert np.equal(predicted_proba, pool_classifiers[0].predict_proba(X)).all()
def test_predict(create_X_y, create_pool_classifiers): X, y = create_X_y pool_classifiers = create_pool_classifiers single_best_test = SingleBest(pool_classifiers=pool_classifiers) single_best_test.fit(X, y) predicted_labels = single_best_test.predict(X) assert np.equal(predicted_labels, 0).all()
def test_not_predict_proba(create_X_y): X, y = create_X_y classifier = MagicMock() classifier.predict.return_value = [0] single_best_test = SingleBest([classifier] * 10) single_best_test.fit(X, y) with pytest.raises(ValueError): single_best_test.predict_proba(X)
def test_fit(create_X_y, create_pool_classifiers): X, y = create_X_y pool_classifiers = create_pool_classifiers single_best_test = SingleBest(pool_classifiers) single_best_test.fit(X, y) assert (single_best_test.best_clf_index_ == 0 or single_best_test.best_clf_index_ == 2)
def test_predict_proba(create_X_y, create_pool_classifiers): X, y = create_X_y pool_classifiers = create_pool_classifiers single_best_test = SingleBest(pool_classifiers) single_best_test.fit(X, y) predicted_proba = single_best_test.predict_proba(X) assert np.equal(predicted_proba, pool_classifiers[0].predict_proba(X)).all()
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) sb = SingleBest(pool) sb.fit(X, y) pred = sb.predict(X) assert np.isin(sb.classes_, pred).all()
def test_fit(create_X_y, create_pool_classifiers): X, y = create_X_y pool_classifiers = create_pool_classifiers single_best_test = SingleBest(pool_classifiers) single_best_test._estimate_performances = MagicMock( return_value=[1.0, 0.5, 0.99]) single_best_test.fit(X, y) assert single_best_test.best_clf_index_ == 0
def test_different_scorer(): X, y = make_classification(n_samples=100, random_state=42) X_val, y_val = make_classification(n_samples=25, random_state=123) pool = AdaBoostClassifier(n_estimators=10).fit(X, y) performances = [] for clf in pool: preds = clf.predict_proba(X_val) performances.append(roc_auc_score(y_val.ravel(), preds[:, -1])) id_best = np.argmax(performances) sb = SingleBest(pool_classifiers=pool, scoring='roc_auc') sb.fit(X_val, y_val) assert id_best == sb.best_clf_index_
def test_single_best(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() single_best = SingleBest(pool_classifiers) single_best.fit(X_dsel, y_dsel) assert np.isclose(single_best.score(X_test, y_test), 0.86969696969696975)