📄 yxcsvmplot.m
字号:
function yxcSVMplot(X, Y, SVIndex, alphaStar, bStar, kernel, sigma, plotAxis)
%
% Plot C-SVC {1, -1} points, and the decision isoline.
%
% Author: Xuchen Yao. yaoxuchen@gmail.com
%
% Input:
% X: num x dim Data, X has num points, every point is described by dim vectors
% Y: n x 1 Data, {1, -1}
% SVIndex: Index for support vectors
% alphaStar: Optimized alpha
% bStar: bias, or primal threshold variable.
% kernel: see yxcSVMkernel
% sigma: see yxcSVMkernel
% plotAxis: the axis of x and y, optional.
[num, dim] = size(X);
if dim ~= 2
return;
end
if num ~= length(Y)
return;
end
Y = Y(:);
plusIndex = find(Y==1);
minusIndex = find(Y==-1);
figure;
plot(X(plusIndex, 1), X(plusIndex, 2), 'r+');
if nargin == 8
axis(plotAxis);
end
hold on;
plot(X(minusIndex, 1), X(minusIndex, 2), 'g.');
hold on;
plot(X(SVIndex, 1), X(SVIndex, 2), 'ko');
hold on;
% % plot the decision isoline
% [cx, cy] = meshgrid(min( X(:, 1)) : .1 : max(X(:, 1)), min( X(:, 2)) : .1 : max(X(:, 2)));
% [ncx, ncy] = size(cx);
% Z = zeros(ncx, ncy);
%
% % My brain drains, don't look at the following garbage code...
% nSV = length(SVIndex);
% for i = 1 : ncx
% for j = 1 : ncy
% for k = 1 : nSV
% Z(i, j) = Z(i, j) + ...
% yxcSVMkernel([cx(i, j) cy(i, j)], X(SVIndex(k), :), kernel, sigma) * ...
% alphaStar(SVIndex(k)) * Y(SVIndex(k));
% end
% Z(i, j) = Z(i, j) + bStar;
% end
% end
% Let's use a smart way.
[cx, cy] = meshgrid(min( X(:, 1)) : .1 : max(X(:, 1)), min( X(:, 2)) : .1 : max(X(:, 2)));
[ncx, ncy] = size(cx);
Xtest = [cx(:) cy(:)];
[YClassified, Z]=yxcSVMclassifer(X, Xtest, Y, alphaStar, bStar, kernel, sigma);
% reshape it.
Z = reshape(Z, ncx, ncy);
% plot the decision isoline
[C,h] = contour(cx, cy, Z, [0,0]);
set(h,'ShowText','on','TextStep',get(h,'LevelStep'));
hold on;
[C,h] = contour(cx, cy, Z, [1,1]);
set(h,'ShowText','on','TextStep',get(h,'LevelStep'));
hold on;
[C,h] = contour(cx, cy, Z, [-1,-1]);
set(h,'ShowText','on','TextStep',get(h,'LevelStep'));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -