Esempio n. 1
0
def test_more_complex_kkt():
    X=numpy.array([
        [1,0],
        [2,1],
        [2,0],
        [4,2],
        [0,1],
        [0,2],
        [1,2],
        [2,8]])
    Y=numpy.array([1,1,1,1,-1,-1,-1,-1])
    C=4.
    kernel=numpy.dot
    Alphas,b=svm_learn_smo(X,Y,kernel,C)

    def verify():
        SVM=(X,Y,Alphas,b,C,kernel)
        sv=numpy.array([1,1,0,0,1,0,1,0])
        nsv=~sv
        computed_sv = (Alphas > 0) & (Alphas < C)
        computed_nsv = ~computed_sv
        assert_kkt(SVM)
        assert numpy.all((sv-computed_sv) >= 0) # computed_sv in sv
        assert numpy.all((computed_nsv-nsv) >= 0) # nsv in computed_nsv
        assert_all_correctly_classified(SVM,X,Y)

    verify()
    Alphas,b=svm_learn_libsvm(X,Y,kernel,C)
    verify()
Esempio n. 2
0
def test_rbf():
    X=numpy.array([
        [0,0,0],
        [1,1,1],
        ])
    Y=numpy.array([ 1, -1 ])
    C=10
    Alphas,b=svm_learn_smo(X,Y,rbf,C)
    SVM=(X,Y,Alphas,b,C,rbf)
    assert_all_correctly_classified(SVM,X,Y)

    Alphas,b=svm_learn_libsvm(X,Y,rbf,C)
    SVM=(X,Y,Alphas,b,C,rbf)
    assert_all_correctly_classified(SVM,X,Y)
Esempio n. 3
0
def test_random():
    R=numpy.random.RandomState(123)
    X=R.rand(10,3)
    X[:5]+=1.
    C=2
    Y=numpy.ones(10)
    Y[5:] *= -1
    Alphas,b=svm_learn_smo(X,Y,rbf,C)
    SVM=(X,Y,Alphas,b,C,rbf)
    assert_more_than_50(SVM,X,Y)

    Alphas,b=svm_learn_libsvm(X,Y,rbf,C)
    SVM=(X,Y,Alphas,b,C,rbf)
    assert_more_than_50(SVM,X,Y)
Esempio n. 4
0
def test_rbf():
    X = numpy.array([
        [0, 0, 0],
        [1, 1, 1],
    ])
    Y = numpy.array([1, -1])
    C = 10
    Alphas, b = svm_learn_smo(X, Y, rbf, C)
    SVM = (X, Y, Alphas, b, C, rbf)
    assert_all_correctly_classified(SVM, X, Y)

    Alphas, b = svm_learn_libsvm(X, Y, rbf, C)
    SVM = (X, Y, Alphas, b, C, rbf)
    assert_all_correctly_classified(SVM, X, Y)
Esempio n. 5
0
def test_random():
    R = numpy.random.RandomState(123)
    X = R.rand(10, 3)
    X[:5] += 1.
    C = 2
    Y = numpy.ones(10)
    Y[5:] *= -1
    Alphas, b = svm_learn_smo(X, Y, rbf, C)
    SVM = (X, Y, Alphas, b, C, rbf)
    assert_more_than_50(SVM, X, Y)

    Alphas, b = svm_learn_libsvm(X, Y, rbf, C)
    SVM = (X, Y, Alphas, b, C, rbf)
    assert_more_than_50(SVM, X, Y)
Esempio n. 6
0
def test_simplest():
    X=numpy.array([
        [1],
        [2],
        ])
    Y=numpy.array([1,-1])
    C=4.
    kernel=numpy.dot
    Alphas,b=svm_learn_smo(X,Y,kernel,C)
    SVM=(X,Y,Alphas,b,C,kernel)
    Alphas_,b_=svm_learn_libsvm(X,Y,kernel,C)
    assert approximate(Alphas,Alphas_)
    assert approximate(b,b_)
    assert approximate(Alphas,[2.,2.])
    assert approximate(b,-3)
    assert_kkt(SVM)
    assert_all_correctly_classified(SVM,X,Y)
Esempio n. 7
0
def test_simplest():
    X = numpy.array([
        [1],
        [2],
    ])
    Y = numpy.array([1, -1])
    C = 4.
    kernel = numpy.dot
    Alphas, b = svm_learn_smo(X, Y, kernel, C)
    SVM = (X, Y, Alphas, b, C, kernel)
    Alphas_, b_ = svm_learn_libsvm(X, Y, kernel, C)
    assert approximate(Alphas, Alphas_)
    assert approximate(b, b_)
    assert approximate(Alphas, [2., 2.])
    assert approximate(b, -3)
    assert_kkt(SVM)
    assert_all_correctly_classified(SVM, X, Y)
Esempio n. 8
0
def test_more_complex_kkt():
    X = numpy.array([[1, 0], [2, 1], [2, 0], [4, 2], [0, 1], [0, 2], [1, 2],
                     [2, 8]])
    Y = numpy.array([1, 1, 1, 1, -1, -1, -1, -1])
    C = 4.
    kernel = numpy.dot
    Alphas, b = svm_learn_smo(X, Y, kernel, C)

    def verify():
        SVM = (X, Y, Alphas, b, C, kernel)
        sv = numpy.array([1, 1, 0, 0, 1, 0, 1, 0])
        nsv = ~sv
        computed_sv = (Alphas > 0) & (Alphas < C)
        computed_nsv = ~computed_sv
        assert_kkt(SVM)
        assert numpy.all((sv - computed_sv) >= 0)  # computed_sv in sv
        assert numpy.all((computed_nsv - nsv) >= 0)  # nsv in computed_nsv
        assert_all_correctly_classified(SVM, X, Y)

    verify()
    Alphas, b = svm_learn_libsvm(X, Y, kernel, C)
    verify()