简体   繁体   English

使用MATLAB和libsvm绘制SVM边距

[英]Plot SVM margins using MATLAB and libsvm

I am using svmlib to classify linearly two dimensional non-separable data. 我正在使用svmlib对二维不可分离的数据进行线性分类。 I am able to train the svm and obtain w and b using svmlib. 我能够训练svm并使用svmlib获得w和b。 Using this information I can plot the decision boundary, along with the support vectors, but I am not sure about how to plot the margins, using the information that svmlib gives me. 使用此信息,我可以绘制决策边界以及支持向量,但是我不确定如何使用svmlib提供给我的信息来绘制边距。

Below is my code: 下面是我的代码:

model = svmtrain(Y,X, '-s 0 -t 0 -c 100');

w = model.SVs' * model.sv_coef;
b = -model.rho;
if (model.Label(1) == -1)
    w = -w; b = -b;
end
y_hat = sign(w'*X' + b);

sv = full(model.SVs);

% plot support vectors
plot(sv(:,1),sv(:,2),'ko', 'MarkerSize', 10);

% plot decision boundary
plot_x = linspace(min(X(:,1)), max(X(:,1)), 30);
plot_y = (-1/w(2))*(w(1)*plot_x + b);
plot(plot_x, plot_y, 'k-', 'LineWidth', 1)

It depends on what you mean by "the margins". 这取决于您所说的“边距”。 It also depends on what SVM version you are talking about (separable on non-separable), but since you mentioned libsvm I'll assume you mean the more general, non-separable version. 它还取决于您所谈论的SVM版本(可分离的版本,不可分离的版本),但是由于您提到libsvm,所以我假设您指的是更通用,不可分离的版本。

The term "margin" can refer to the Euclidean distance from the separating hyperplane to the hyperplane defined by wx+b=1 (or wx+b=-1 ). 术语“裕度”可以指从分离的超平面到由wx+b=1 (或wx+b=-1 )定义的超平面的欧几里得距离。 This distance is given by 1/norm(w) . 该距离由1/norm(w)

"Margin" can also refer to the margin of a specific sample x , which is the Euclidean distance of x from the separating hyperplane. “裕度”也可以指特定样本x的裕度,它是x与分离超平面的欧几里得距离。 It is given by 它由

(wx+b)/norm(w) (wx + b)/范数(w)

note that this is a signed distance, that is it is negative/positive, depending on which side of the hyperplane the point x resides. 注意,这是一个有符号的距离,即它是负/正,这取决于点x所在的超平面的哪一侧。 You can draw it as a line from the point, perpendicular to the hyperplane. 您可以从垂直于超平面的点将其绘制为一条线。

Another interesting value is the slack variable xi , which is the "algebraic" distance (not Euclidean) of a support vector from the "hard" margin defined by wx+b=+1 (or -1 ). 另一个有趣的值是松弛变量xi ,它是支撑向量与wx+b=+1 (或-1 )定义的“硬”边界之间的“代数”距离(不是欧几里得)。 It is positive only for support vectors, and if a point is not a support vector, its xi equals 0. More compactly: 它仅对支持向量为正,并且如果一个点不是支持向量,则其xi等于0。

xi = max(0, 1 - y*(w'*x+b)) xi =最大值(0,1-y *(w'* x + b))

where y is the label. y是标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM