用Python中的LIBSVM支持向量和系数计算超平面方程

2024-03-29 08:09:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python中的LIBSVM库,并尝试从计算出的支持向量重建超平面的方程(w'x+b)。你知道吗

该模型似乎训练正确,但我无法手动计算预测结果相匹配的svm\U预测测试数据的输出。你知道吗

我已经使用下面的链接从常见问题来尝试和排除故障,但我仍然无法计算出正确的结果。https://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f804

我的代码如下:

from svmutil import *
import numpy as np

ytrain, xtrain = svm_read_problem('small_train.libsvm')
# Change labels from 0 to -1    
for index in range(len(ytrain)):
    if ytrain[index] == 0:
        ytrain[index] = -1.0
print ("Training set loaded...")

m = svm_train(ytrain, xtrain, '-q')
print ("Model trained...")

sv = np.asarray(m.get_SV())
sv_coef = np.asarray(m.get_sv_coef())
sv_indices = np.asarray(m.get_sv_indices())
rho = m.rho[0]

w = np.zeros(len(xtrain[0]))
b = -rho
# weight vector w = sum over i ( coefsi * xi )
for index, coef in zip(sv_indices, sv_coef):
    ai = coef[0]
    for key in xtrain[index-1]:
        w[key] = w[key] + (ai * xtrain[index-1][key])

# From LIBSVM FAQ - Doesn't seem to impact results
# if m.label[1] == -1:
#     w = np.negative(w)
#     b = -b

print(np.round(w,2))

ytest, xtest = svm_read_problem('small_test.libsvm')
# Change labels from 0 to -1  
for index in range(len(ytest)):
    if ytest[index] == 0:
        ytest[index] = -1.0

print ("Test set loaded...")
print ("Predict test set...")
p_label, p_acc, p_val = svm_predict(ytest, xtest, m)

print("p_label: ", p_label)
print("p_val: ", np.round(p_val,3))

for i in range(len(ytest)):
    wx = 0
    for key in xtest[i]:
        wx = wx + (xtest[i][key] * w[key])
    print("Manual calc: ", np.round(wx + b,3))

我的理解是,我用wx+b手工计算的结果应该与p值中包含的结果相匹配。我已经尝试否定w和b,但仍然无法得到与p值中相同的结果

我使用的数据集(LIBSVM格式)是:

小_训练.libsvm你知道吗

0 0:-0.36 1:-0.91 2:-0.99 3:-0.57 4:-1.38 5:-1.54
1 0:-1.4 1:-1.9 2:0.09 3:0.29 4:-0.3 5:-1.3
1 0:-0.43 1:1.45 2:-0.68 3:-1.58 4:0.32 5:-0.14
1 0:-0.76 1:0.3 2:-0.57 3:-0.33 4:-1.5 5:1.84

小_测试.libsvm你知道吗

1 0:-0.97 1:-0.69 2:-0.96 3:1.05 4:0.02 5:0.64
0 0:-0.82 1:-0.17 2:-0.36 3:-1.99 4:-1.54 5:-0.31

w的值计算正确吗?p值结果是否是要比较的正确值?你知道吗

如有任何帮助,我们将不胜感激。你知道吗


Tags: keyinforindexlennpwxprint
1条回答
网友
1楼 · 发布于 2024-03-29 08:09:10

我设法通过更改以下内容使值匹配:

m = svm_train(ytrain, xtrain, '-q')

m = svm_train(ytrain, xtrain, '-q -t 0')

从文档来看,默认的内核类型是非线性的(径向基函数)。设置线性核后,结果现在看起来是对齐的。你知道吗

以下是可用的内核类型:

-t kernel_type : set type of kernel function (default 2)
    0   linear: u'*v
    1   polynomial: (gamma*u'*v + coef0)^degree
    2   radial basis function: exp(-gamma*|u-v|^2)
    3   sigmoid: tanh(gamma*u'*v + coef0)
    4   precomputed kernel (kernel values in training_set_file)

相关问题 更多 >