Repository: scikit-learn/scikit-learn
File: sklearn/semi_supervised/tests/test_self_training.py (Python)

Code before:
        assert msg.format(i, 10) in captured.out

    assert msg.format(n_expected_iter, (n_samples - 1) % 10) in captured.out


def test_k_best_selects_best():
    # Tests that the labels added by st really are the 10 best labels.
    est = LogisticRegression(random_state=0)
    st = SelfTrainingClassifier(est, criterion="k_best", max_iter=1, k_best=10)
    has_label = y_train_missing_labels != -1
    st.fit(X_train, y_train_missing_labels)

    got_label = ~has_label & (st.transduction_ != -1)

    est.fit(X_train[has_label], y_train_missing_labels[has_label])
    pred = est.predict_proba(X_train[~has_label])
    max_proba = np.max(pred, axis=1)

    most_confident_est = X_train[~has_label][np.argsort(max_proba)[-10:]]
    added_by_st = X_train[np.where(got_label)].tolist()

    for row in most_confident_est.tolist():
        assert row in added_by_st


@pytest.mark.filterwarnings("ignore::FutureWarning")
def test_estimator_meta_estimator():
    # Check that a meta-estimator relying on an estimator implementing
    # `predict_proba` will work even if it does not expose this method before being
    # fitted.
    # Non-regression test for:
    # https://github.com/scikit-learn/scikit-learn/issues/19119

    estimator = StackingClassifier(
        estimators=[
            ("svc_1", SVC(probability=True)),
            ("svc_2", SVC(probability=True)),
        ],
        final_estimator=SVC(probability=True),
        cv=2,
    )

    assert hasattr(estimator, "predict_proba")
    clf = SelfTrainingClassifier(estimator=estimator)
    clf.fit(X_train, y_train_missing_labels)
    clf.predict_proba(X_test)

    estimator = StackingClassifier(
        estimators=[
            ("svc_1", SVC(probability=False)),
            ("svc_2", SVC(probability=False)),

Review comment (suggestion):
Here we can probably use clf_1 and clf_2 set to `LogisticRegression()` in the first half of the test and use `svc_1` and `svc_2`  set to `SVC()` (with the implicit default value for the `probability` parameter) instead of hiding the warning.

Code after:

    assert msg.format(n_expected_iter, (n_samples - 1) % 10) in captured.out


def test_k_best_selects_best():
    # Tests that the labels added by st really are the 10 best labels.
    est = LogisticRegression(random_state=0)
    st = SelfTrainingClassifier(est, criterion="k_best", max_iter=1, k_best=10)
    has_label = y_train_missing_labels != -1
    st.fit(X_train, y_train_missing_labels)

    got_label = ~has_label & (st.transduction_ != -1)

    est.fit(X_train[has_label], y_train_missing_labels[has_label])
    pred = est.predict_proba(X_train[~has_label])
    max_proba = np.max(pred, axis=1)

    most_confident_est = X_train[~has_label][np.argsort(max_proba)[-10:]]
    added_by_st = X_train[np.where(got_label)].tolist()

    for row in most_confident_est.tolist():
        assert row in added_by_st


def test_estimator_meta_estimator():
    # Check that a meta-estimator relying on an estimator implementing
    # `predict_proba` will work even if it does not expose this method before being
    # fitted.
    # Non-regression test for:
    # https://github.com/scikit-learn/scikit-learn/issues/19119

    estimator = StackingClassifier(
        estimators=[
            ("clf_1", LogisticRegression()),
            ("clf_2", LogisticRegression()),
        ],
        final_estimator=LogisticRegression(),
        cv=2,
    )

    assert hasattr(estimator, "predict_proba")
    clf = SelfTrainingClassifier(estimator=estimator)
    clf.fit(X_train, y_train_missing_labels)
    clf.predict_proba(X_test)

    estimator = StackingClassifier(
        estimators=[
            ("svc_1", SVC()),
            ("svc_2", SVC()),
        ],
        final_estimator=SVC(),