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

Code before:
T = [[-1, -1], [2, 2], [3, 2]]
true_result = [1, 2, 2]


def get_iris_dataset(random_seed):
    iris = datasets.load_iris()
    rng = check_random_state(random_seed)
    perm = rng.permutation(iris.target.size)
    iris.data = iris.data[perm]
    iris.target = iris.target[perm]
    return iris


def test_libsvm_parameters():
    # Test parameters on classes that make use of libsvm.
    clf = svm.SVC(kernel="linear").fit(X, Y)
    assert_array_equal(clf.dual_coef_, [[-0.25, 0.25]])
    assert_array_equal(clf.support_, [1, 3])
    assert_array_equal(clf.support_vectors_, (X[1], X[3]))
    assert_array_equal(clf.intercept_, [0.0])
    assert_array_equal(clf.predict(X), Y)


# XXX: this test is thread-unsafe because it uses _libsvm.cross_validation:
# https://github.com/scikit-learn/scikit-learn/issues/31885
# TODO(1.10): investigate why assertion on L148 fails.
@pytest.mark.thread_unsafe
def test_libsvm_iris(global_random_seed):
    # Check consistency on dataset iris.
    iris = get_iris_dataset(global_random_seed)
    # shuffle the dataset so that labels are not ordered
    for k in ("linear", "rbf"):
        clf = svm.SVC(kernel=k).fit(iris.data, iris.target)
        assert np.mean(clf.predict(iris.data) == iris.target) > 0.9
        assert hasattr(clf, "coef_") == (k == "linear")

    assert_array_equal(clf.classes_, np.sort(clf.classes_))

    # check also the low-level API
    # We unpack the values to create a dictionary with some of the return values
    # from Libsvm's fit.
    (
        libsvm_support,
        libsvm_support_vectors,
        libsvm_n_class_SV,
        libsvm_sv_coef,
        libsvm_intercept,
        libsvm_probA,
        libsvm_probB,
        # libsvm_fit_status and libsvm_n_iter won't be used below.
        libsvm_fit_status,

Review comment (bug):
```suggestion
# TODO(1.11): investigate why assertion on L148 fails.
```

Code after:
T = [[-1, -1], [2, 2], [3, 2]]
true_result = [1, 2, 2]


def get_iris_dataset(random_seed):
    iris = datasets.load_iris()
    rng = check_random_state(random_seed)
    perm = rng.permutation(iris.target.size)
    iris.data = iris.data[perm]
    iris.target = iris.target[perm]
    return iris


def test_libsvm_parameters():
    # Test parameters on classes that make use of libsvm.
    clf = svm.SVC(kernel="linear").fit(X, Y)
    assert_array_equal(clf.dual_coef_, [[-0.25, 0.25]])
    assert_array_equal(clf.support_, [1, 3])
    assert_array_equal(clf.support_vectors_, (X[1], X[3]))
    assert_array_equal(clf.intercept_, [0.0])
    assert_array_equal(clf.predict(X), Y)


# XXX: this test is thread-unsafe because it uses _libsvm.cross_validation:
# https://github.com/scikit-learn/scikit-learn/issues/31885
# TODO: investigate why assertion on L148 fails.
@pytest.mark.thread_unsafe
def test_libsvm_iris(global_random_seed):
    # Check consistency on dataset iris.
    iris = get_iris_dataset(global_random_seed)
    # shuffle the dataset so that labels are not ordered
    for k in ("linear", "rbf"):
        clf = svm.SVC(kernel=k).fit(iris.data, iris.target)
        assert np.mean(clf.predict(iris.data) == iris.target) > 0.9
        assert hasattr(clf, "coef_") == (k == "linear")

    assert_array_equal(clf.classes_, np.sort(clf.classes_))

    # check also the low-level API
    # We unpack the values to create a dictionary with some of the return values
    # from Libsvm's fit.
    (
        libsvm_support,
        libsvm_support_vectors,
        libsvm_n_class_SV,
        libsvm_sv_coef,
        libsvm_intercept,
        libsvm_probA,
        libsvm_probB,
        # libsvm_fit_status and libsvm_n_iter won't be used below.
        libsvm_fit_status,