LIBSVM与预先计算的内核的python绑定和交叉验证

时间:2015-12-19 14:49:09

标签: python scikit-learn svm libsvm

我尝试通过Python绑定使用libsvm和预先计算的克数矩阵(numpy数组)。我使用10倍交叉验证来计算准确度。由于某种原因,计算的控制与文献中的控制不匹配。

注意:C参数不是问题。此外,由于以下问题,我没有使用sklearn [1]。

有什么想法吗?

import numpy as np
from sklearn import cross_validation

from svmutil import *

def compute_accuracy(gram_matrix, data, k=10):

kv = cross_validation.KFold(len(gram_matrix), n_folds=k, shuffle=True)
s = 0.0

for train_index, test_index in kv:

    gm_train = gram_matrix[train_index, :]
    gm_train = gm_train[:, train_index]
    data_train = data[train_index]

    gm_test = gram_matrix[test_index, :]
    gm_test = gm_test[:, test_index]
    data_test = data[test_index]

    # Have to use libsvm directly here, because of a bug in sklearn with precomputed gram matrices
    x = []
    for i in range(len(gm_train)):
        l = gm_train[i].tolist()
        l.insert(0, i + 1)
        x.append(l)

    prob = svm_problem(data_train.tolist(), x, isKernel=True)
    param = svm_parameter('-t 4 -c 10 -q')
    m = svm_train(prob, param)

    xx = []
    for i in range(len(gm_test)):
        t = gm_test[i].tolist()
        t.insert(0, i + 1)
        xx.append(t)

    p_label, p_acc, p_val = svm_predict(data_test.tolist(), xx, m)

    ACC, MSE, SCC = evaluations(p_label, data_test.tolist())
    print ACC
    s += ACC

return s / k

[1] Nested cross-validation in grid search for precomputed kernels in scikit-learn

0 个答案:

没有答案