Repository: scikit-learn/scikit-learn
File: sklearn/svm/tests/test_sparse.py (Python)

Code before:

    assert_array_almost_equal(
        dense_svm.decision_function(X_test_dense), sparse_svm.decision_function(X_test)
    )
    assert_array_almost_equal(
        dense_svm.decision_function(X_test_dense),
        sparse_svm.decision_function(X_test_dense),
    )
    if isinstance(dense_svm, svm.OneClassSVM):
        msg = "cannot use sparse input in 'OneClassSVM' trained on dense data"
    else:
        if hasattr(dense_svm, "predict_proba"):
            assert_array_almost_equal(
                dense_svm.predict_proba(X_test_dense),
                sparse_svm.predict_proba(X_test),
                decimal=4,
            )
        msg = "cannot use sparse input in 'SVC' trained on dense data"
    if sparse.issparse(X_test):
        with pytest.raises(ValueError, match=msg):
            dense_svm.predict(X_test)


# XXX: probability=True is not thread-safe:
# https://github.com/scikit-learn/scikit-learn/issues/31885
# TODO(1.10): remove probability=True and adapt check_svm_model_equal accordingly.
@pytest.mark.thread_unsafe
@skip_if_32bit
@pytest.mark.parametrize(
    "X_train, y_train, X_test",
    [
        [X, Y, T],
        [X2, Y2, T2],
        [X_blobs[:80], y_blobs[:80], X_blobs[80:]],
        [iris.data, iris.target, iris.data],
    ],
)
@pytest.mark.parametrize("kernel", ["linear", "poly", "rbf", "sigmoid"])
@pytest.mark.parametrize("sparse_container", CSR_CONTAINERS + LIL_CONTAINERS)
@pytest.mark.filterwarnings("ignore::FutureWarning")
def test_svc(X_train, y_train, X_test, kernel, sparse_container):
    """Check that sparse SVC gives the same result as SVC."""
    X_train = sparse_container(X_train)

    clf = svm.SVC(
        gamma=1,
        kernel=kernel,
        probability=True,
        random_state=0,
        decision_function_shape="ovo",
    )

Review comment (suggestion):
```suggestion
# TODO(1.11): remove probability=True and adapt check_svm_model_equal accordingly.
```

Code after:

    assert_array_almost_equal(
        dense_svm.decision_function(X_test_dense), sparse_svm.decision_function(X_test)
    )
    assert_array_almost_equal(
        dense_svm.decision_function(X_test_dense),
        sparse_svm.decision_function(X_test_dense),
    )
    if isinstance(dense_svm, svm.OneClassSVM):
        msg = "cannot use sparse input in 'OneClassSVM' trained on dense data"
    else:
        if hasattr(dense_svm, "predict_proba"):
            assert_array_almost_equal(
                dense_svm.predict_proba(X_test_dense),
                sparse_svm.predict_proba(X_test),
                decimal=4,
            )
        msg = "cannot use sparse input in 'SVC' trained on dense data"
    if sparse.issparse(X_test):
        with pytest.raises(ValueError, match=msg):
            dense_svm.predict(X_test)


# XXX: probability=True is not thread-safe:
# https://github.com/scikit-learn/scikit-learn/issues/31885
# TODO(1.11): remove probability=True and adapt check_svm_model_equal accordingly.
@pytest.mark.thread_unsafe
@skip_if_32bit
@pytest.mark.parametrize(
    "X_train, y_train, X_test",
    [
        [X, Y, T],
        [X2, Y2, T2],
        [X_blobs[:80], y_blobs[:80], X_blobs[80:]],
        [iris.data, iris.target, iris.data],
    ],
)
@pytest.mark.parametrize("kernel", ["linear", "poly", "rbf", "sigmoid"])
@pytest.mark.parametrize("sparse_container", CSR_CONTAINERS + LIL_CONTAINERS)
@pytest.mark.filterwarnings("ignore::FutureWarning")
def test_svc(X_train, y_train, X_test, kernel, sparse_container):
    """Check that sparse SVC gives the same result as SVC."""
    X_train = sparse_container(X_train)

    clf = svm.SVC(
        gamma=1,
        kernel=kernel,
        probability=True,
        random_state=0,
        decision_function_shape="ovo",
    )