<commit_before>import numpy as np
from scikits.learn import svm
from numpy.testing import assert_array_equal, \
                          assert_array_almost_equal, \
                          assert_raises

# Data is just 6 separable points in the plane
X = np.array( [[-2,-1], [-1, -1], [-1, -2], [1,1], [1,2], [2, 1]])
Y = np.array( [1, 1, 1, 2, 2, 2])
T = np.array( [[-1,-1], [2, 2], [3, 2]] )


clf = svm.SVC(probability=True)
clf.fit(X, Y)
# print clf.probA_
# print clf.probB_
# clf.predict(T)
print clf.prob_predict(T)


def test_svm_params():
    """
    C_SVC algorithm and linear kernel.

    This checks that we retrieve the correct parameters.
    """

    clf =  svm.SVC(kernel='linear')
    clf.fit(X, Y)

    assert_array_equal(clf.coef_, [[ 0.25, -.25]])
    assert_array_equal(clf.support_, [[-1,-1], [1, 1]])
    assert_array_equal(clf.rho_, [0.])

def test_fit():
    clf = svm.SVC()
    clf.fit([[1,2]], [0])
    assert_array_equal(clf.predict([[-1, -1]]), [0.])

def test_tweak_params():
    """
    Make sure some tweaking of parameters works.

    We change clf.coef_ at run time and expect .predict() to change
    accordingly. Notice that this is not trivial since it involves a lot
    of C/Python copying in the libsvm bindings.

    The success of this test ensures that the mapping between libsvm and
    the python classifier is complete.
    """
    clf = svm.SVC(kernel='linear')
    clf.fit(X, Y)
    assert_array_equal(clf.coef_, [[.25, -.25]])
    assert_array_equal(clf.predict([[-.1, -.1]]), [1])
    clf.coef_ = np.array([[.0, 1.]])
    assert_array_equal(clf.predict([[-.1, -.1]]), [2])

def test_error():
    """
    Test that it gives proper exception on deficient input
    """
    # impossible value of nu
    clf = svm.SVC(impl='nu_svc', kernel='linear', nu=0.0)
    assert_raises(ValueError, clf.fit, X, Y)

    Y2 = Y[:-1] # wrong dimensions for labels
    assert_raises(ValueError, svm.SVC, X, Y2)

def test_predict():
    true_result = [1, 2, 2]
    clf = svm.SVC()
    clf.fit(X, Y)
    assert_array_equal(clf.predict(T), true_result)



def test_probability():
    """
    predict probabilities
    """
    clf = svm.SVC(probability=True)
    clf.fit(X, Y)
    assert_array_almost_equal(clf.prob_predict(T), [])


def test_noncontiguous():
    """
    Test with arrays that are non-contiguous.
    """
    Xt = X.transpose()
    Yt = [1, 2]
    clf = svm.SVC()
    clf.fit(Xt, Yt)
    assert_array_equal(clf.predict(T), [1, 2, 2])

def test_predict_multiclass():
    """
    this example is from libsvm/README
    """
    X  =   [[0,	  0.1,	  0.2,	  0,	  0],
            [ 0,  0.1,	  0.3,	 -1.2,	  0],
            [0.4,  0,	  0,	  0,	  0],
            [0,	  0.1,	  0,	  1.4,	  0.5],
            [-0.1,-0.2,	  0.1,	  1.1,	  0.1]]<commit_msg>Implement a test for SVC probability estimates.<commit_after>import numpy as np
from scikits.learn import svm
from numpy.testing import assert_array_equal, \
                          assert_array_almost_equal, \
                          assert_raises

# Data is just 6 separable points in the plane
X = np.array( [[-2,-1], [-1, -1], [-1, -2], [1,1], [1,2], [2, 1]])
Y = np.array( [1, 1, 1, 2, 2, 2])
T = np.array( [[-1,-1], [2, 2], [3, 2]] )


clf = svm.SVC(probability=True)
clf.fit(X, Y)
# print clf.probA_
# print clf.probB_
# clf.predict(T)
print clf.prob_predict(T)


def test_svm_params():
    """
    C_SVC algorithm and linear kernel.

    This checks that we retrieve the correct parameters.
    """

    clf =  svm.SVC(kernel='linear')
    clf.fit(X, Y)

    assert_array_equal(clf.coef_, [[ 0.25, -.25]])
    assert_array_equal(clf.support_, [[-1,-1], [1, 1]])
    assert_array_equal(clf.rho_, [0.])

def test_fit():
    clf = svm.SVC()
    clf.fit([[1,2]], [0])
    assert_array_equal(clf.predict([[-1, -1]]), [0.])

def test_tweak_params():
    """
    Make sure some tweaking of parameters works.

    We change clf.coef_ at run time and expect .predict() to change
    accordingly. Notice that this is not trivial since it involves a lot
    of C/Python copying in the libsvm bindings.

    The success of this test ensures that the mapping between libsvm and
    the python classifier is complete.
    """
    clf = svm.SVC(kernel='linear')
    clf.fit(X, Y)
    assert_array_equal(clf.coef_, [[.25, -.25]])
    assert_array_equal(clf.predict([[-.1, -.1]]), [1])
    clf.coef_ = np.array([[.0, 1.]])
    assert_array_equal(clf.predict([[-.1, -.1]]), [2])

def test_error():
    """
    Test that it gives proper exception on deficient input
    """
    # impossible value of nu
    clf = svm.SVC(impl='nu_svc', kernel='linear', nu=0.0)
    assert_raises(ValueError, clf.fit, X, Y)

    Y2 = Y[:-1] # wrong dimensions for labels
    assert_raises(ValueError, svm.SVC, X, Y2)

def test_predict():
    true_result = [1, 2, 2]
    clf = svm.SVC()
    clf.fit(X, Y)
    assert_array_equal(clf.predict(T), true_result)



def test_probability():
    """
    predict probabilities
    """
    clf = svm.SVC(probability=True)
    clf.fit(X, Y)
    assert_array_almost_equal(clf.prob_predict(T),
                              [[ 0.81937173,  0.18062827],
                               [ 0.18921888,  0.81078112],
                               [ 0.2769648 ,  0.7230352 ]])


def test_noncontiguous():
    """
    Test with arrays that are non-contiguous.
    """
    Xt = X.transpose()
    Yt = [1, 2]
    clf = svm.SVC()
    clf.fit(Xt, Yt)
    assert_array_equal(clf.predict(T), [1, 2, 2])

def test_predict_multiclass():
    """
    this example is from libsvm/README
    """
    X  =   [[0,	  0.1,	  0.2,	  0,	  0],
            [ 0,  0.1,	  0.3,	 -1.2,	  0],
            [0.4,  0,	  0,	  0,	  0],
            [0,	  0.1,	  0,	  1.4,	  0.5],
            [-0.1,-0.2,	  0.1,	  1.1,	  0.1]]
{'fraction_non_alphanumeric': 0.1630397236614853, 'fraction_numerical': 0.045250431778929186, 'mean_word_length': 2.3609982588508416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2739723', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2294, 'n_words': 563}