Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different accuracy for LibSVM and scikit-learn

For the same dataset and parameters I get different accuracy for LibSVM and scikit-learn's SVM implementation, even though scikit-learn also uses LibSVM internally.

What did I overlook?

LibSVM command line version:

me@my-compyter:~/Libraries/libsvm-3.16$ ./svm-train -c 1 -g 0.07 heart_scale heart_scale.model
optimization finished, #iter = 134
nu = 0.433785
obj = -101.855060, rho = 0.426412
nSV = 130, nBSV = 107
Total nSV = 130
me@my-compyter:~/Libraries/libsvm-3.16$ ./svm-predict heart_scale heart_scale.model heart_scale.result
Accuracy = 86.6667% (234/270) (classification)

Scikit-learn NuSVC version:

In [1]: from sklearn.datasets import load_svmlight_file    
In [2]: X_train, y_train = load_svmlight_file('heart_scale')    
In [3]: from sklearn import svm    
In [4]: clf = svm.NuSVC(gamma=0.07,verbose=True)   
In [5]: clf.fit(X_train,y_train)
        [LibSVM]*
        optimization finished, #iter = 118
        C = 0.479830
        obj = 9.722436, rho = -0.224096
        nSV = 145, nBSV = 125
        Total nSV = 145
Out[5]: NuSVC(cache_size=200, coef0=0.0, degree=3, gamma=0.07, kernel='rbf',
        max_iter=-1, nu=0.5, probability=False, shrinking=True, tol=0.001,
        verbose=True)
In [6]: pred = clf.predict(X_train)    
In [7]: from sklearn.metrics import accuracy_score    
In [8]: accuracy_score(y_train, pred)
Out[8]: 0.8481481481481481

Scikit-learn SVC version:

In [1]: from sklearn.datasets import load_svmlight_file    
In [2]: X_train, y_train = load_svmlight_file('heart_scale')    
In [3]: from sklearn import svm    
In [4]: clf = svm.SVC(gamma=0.07,C=1, verbose=True)   
In [5]: clf.fit(X_train,y_train)
        [LibSVM]*
        optimization finished, #iter = 153
        obj = -101.855059, rho = -0.426465
        nSV = 130, nBSV = 107
        Total nSV = 130
Out[5]: SVC(C=1, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.07,
        kernel='rbf', max_iter=-1, probability=False, shrinking=True, tol=0.001,
        verbose=True)
In [6]: pred = clf.predict(X_train)    
In [7]: from sklearn.metrics import accuracy_score    
In [8]: accuracy_score(y_train, pred)
Out[8]: 0.8666666666666667

Updates

Update1: updated the scikit-learn example from SVR to NuSVC, see ogrisel's answer

Update2: added output for verbose=True

Update3: added a scikit-learn SVC version

So it looks like my problem is solved. If I use SVC with C=1 and not NuSVC I get the same results as libsvm, but can somebody explain why NuSVC and SVC(C=1) give different results, even though, they should do the same (see ogrisel's answer)?

like image 673
Framester Avatar asked Mar 06 '13 17:03

Framester


1 Answers

SVR is a regression model, not a classification model. svm-train -c 1 is the Nu-SVC model that is available as sklearn.svm.NuSVC class.

like image 80
ogrisel Avatar answered Sep 30 '22 16:09

ogrisel