gpt4 book ai didi

libsvm - LibSVM 和 scikit-learn 的准确度不同

转载 作者:行者123 更新时间:2023-12-01 18:12:36 58 4
gpt4 key购买 nike

对于相同的数据集和参数,LibSVMscikit-learn 的 SVM 实现的精度不同,尽管 scikit-learn also uses LibSVM internally

我忽略了什么?

LibSVM 命令行版本:

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 版本:

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 版本:

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

更新

更新1:将 scikit-learn 示例从 SVR 更新为 NuSVC,请参阅 ogrisel 的回答

更新2:添加了verbose=True的输出

更新3:添加了 scikit-learn SVC 版本

看来我的问题已经解决了。如果我使用带有 C=1 的 SVC 而不是 NuSVC,我会得到与 libsvm 相​​同的结果,但是有人可以解释为什么 NuSVC 和 SVC(C=1) 给出不同的结果,尽管他们应该这样做相同(参见 ogrisel 的答案)?

最佳答案

SVR 是回归模型,而不是分类模型。 svm-train -c 1 是 Nu-SVC 模型,可作为 sklearn.svm.NuSVC 类使用。

关于libsvm - LibSVM 和 scikit-learn 的准确度不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15254243/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com