Repository: scikit-learn/scikit-learn
File: sklearn/linear_model/tests/test_sgd.py (Python)

Code before:
    )
    pipe_sgd = make_pipeline(transform, clf_sgd)
    pipe_sgd.fit(X_train)
    y_pred_sgdocsvm = pipe_sgd.predict(X_test)
    dec_sgdocsvm = pipe_sgd.decision_function(X_test).reshape(1, -1)

    assert np.mean(y_pred_sgdocsvm == y_pred_ocsvm) >= 0.99
    corrcoef = np.corrcoef(np.concatenate((dec_ocsvm, dec_sgdocsvm)))[0, 1]
    assert corrcoef >= 0.9


def test_sgd_oneclass_convergence():
    # Check that the optimization does not end early,
    # that the stopping criterion is working
    for nu in [0.1, 0.5, 0.9]:
        # no need for large max_iter
        model = SGDOneClassSVM(
            nu=nu, max_iter=100, tol=1e-3, learning_rate="constant", eta0=1e-3
        )
        model.fit(iris.data)
        # 6 is the minimal number of iterations, after which optimization can stop
        assert model.n_iter_ > 6


def test_sgd_oneclass_vs_linear_oneclass():
    # Test convergence vs. liblinear OCSVM with kernel="linear"
    for nu in [0.1, 0.5, 0.9]:
        # allow enough iterations, small dataset
        model = SGDOneClassSVM(
            nu=nu, max_iter=20000, tol=None, learning_rate="constant", eta0=1e-3
        )
        model_ref = OneClassSVM(kernel="linear", nu=nu, tol=1e-6)  # reference model
        model.fit(iris.data)
        model_ref.fit(iris.data)

        preds = model.predict(iris.data)
        dec_fn = model.decision_function(iris.data)

        preds_ref = model_ref.predict(iris.data)
        dec_fn_ref = model_ref.decision_function(iris.data)

        dec_fn_corr = np.corrcoef(dec_fn, dec_fn_ref)[0, 1]
        preds_corr = np.corrcoef(preds, preds_ref)[0, 1]
        # check weights and intercept concatenated together for correlation
        coef_corr = np.corrcoef(
            np.concatenate([model.coef_, -model.offset_]),
            np.concatenate([model_ref.coef_.flatten(), model_ref.intercept_]),
        )[0, 1]
        # share of predicted 1's
        share_ones = (preds == 1).sum() / len(preds)
        share_ones_ref = (preds_ref == 1).sum() / len(preds_ref)

Review comment (suggestion):
```suggestion
    # Test convergence vs. liblinear `OneClassSVM` with kernel="linear"
```

Code after:
    )
    pipe_sgd = make_pipeline(transform, clf_sgd)
    pipe_sgd.fit(X_train)
    y_pred_sgdocsvm = pipe_sgd.predict(X_test)
    dec_sgdocsvm = pipe_sgd.decision_function(X_test).reshape(1, -1)

    assert np.mean(y_pred_sgdocsvm == y_pred_ocsvm) >= 0.99
    corrcoef = np.corrcoef(np.concatenate((dec_ocsvm, dec_sgdocsvm)))[0, 1]
    assert corrcoef >= 0.9


def test_sgd_oneclass_convergence():
    # Check that the optimization does not end early and that the stopping criterion
    # is working. Non-regression test for #30027
    for nu in [0.1, 0.5, 0.9]:
        # no need for large max_iter
        model = SGDOneClassSVM(
            nu=nu, max_iter=100, tol=1e-3, learning_rate="constant", eta0=1e-3
        )
        model.fit(iris.data)
        # 6 is the minimal number of iterations that should be surpassed, after which
        # the optimization can stop
        assert model.n_iter_ > 6


def test_sgd_oneclass_vs_linear_oneclass():
    # Test convergence vs. liblinear `OneClassSVM` with kernel="linear"
    for nu in [0.1, 0.5, 0.9]:
        # allow enough iterations, small dataset
        model = SGDOneClassSVM(
            nu=nu, max_iter=20000, tol=None, learning_rate="constant", eta0=1e-3
        )
        model_ref = OneClassSVM(kernel="linear", nu=nu, tol=1e-6)  # reference model
        model.fit(iris.data)
        model_ref.fit(iris.data)

        preds = model.predict(iris.data)
        dec_fn = model.decision_function(iris.data)

        preds_ref = model_ref.predict(iris.data)
        dec_fn_ref = model_ref.decision_function(iris.data)

        dec_fn_corr = np.corrcoef(dec_fn, dec_fn_ref)[0, 1]
        preds_corr = np.corrcoef(preds, preds_ref)[0, 1]
        # check weights and intercept concatenated together for correlation
        coef_corr = np.corrcoef(
            np.concatenate([model.coef_, -model.offset_]),
            np.concatenate([model_ref.coef_.flatten(), model_ref.intercept_]),
        )[0, 1]
        # share of predicted 1's
        share_ones = (preds == 1).sum() / len(preds)