我有一个由自定义内核创建的数据集的预计算的Gram矩阵。
Gram矩阵以libsvm格式存储。在
我想用SVC分类器计算10倍交叉验证给出的平均精度,C=10。在
从命令行使用libsvm时,我会:svm-train -t 4 -v 10 -c 10 gram.libsvm
我收到这个输出
^{pr2}$
现在,我想使用sklearn库在Python中复制相同的值。我知道sklearn应该在libsvm上实现,所以我期待同样的结果。在
在Python中,我有以下代码:#gram1 is the Gram matrix of my custom kernel and y are the labels
clf = svm.SVC(C=10,kernel='precomputed')
scores = cross_validation.cross_val_score(clf, gram1, y, cv=10, scoring='accuracy')
print scores.mean()
结果是0.788786616898
我该怎么解决这个问题?在
更新1.0
我继续在sklearn和libsvm之间有不同的cv精度值。我检查了两个版本的数据是一样的。我决定把我的数据放到网上。这是gram matrix,这是gram matrix in libsvm format,这些是{a3}。在
在libsvm上,我运行以下命令:svm-train -t 4 -v 10 -c 10 gram.libsvm
我得到这个输出:Cross Validation Accuracy = 79.2553%
在Python sklearn上,我运行以下代码:from sklearn.datasets import load_svmlight_file
yl=load_target("labels.target",'file')
gram1=np.loadtxt("gram.mtx")
print gram1.mean()
x, y = load_svmlight_file("gram.libsvm")
print x.shape
print x[0,:]
x=x.todense()[:,1:]
print np.array_equal(gram1,x)
print gram1[0,:]
print np.array_equal(y, yl)
clf = svm.SVC(C=10,kernel='precomputed')
scores = cross_validation.cross_val_score(clf, gram1, y, cv=10, scoring='accuracy')
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
print scores.mean()
我得到0.788538011696的准确度。在
这与我得到的其他结果相比有一点差距,有时我在libsvm和sklearn的结果之间有8点的差距。在
我使用的是0.16版本的sklearn和libsvm工具3.12-1。在