我想在此链接中绘制libsvmtrain_ova
的结果:
10 fold cross-validation in one-against-all SVM (using LibSVM)
,我在libsvmtrain_ova
中使用了此代码,但我认为它无法正常运行。
hold off
figure();
for j=1:numLabels
w = models{j}.SVs' * models {j}.sv_coef;
b = -models{j}.rho;
c1 = find(double(labels==1) == 1);
c2= find(double(labels==2) == 1);
c3=find(double(labels==3) == 1);
plot(X(c1,1), X(c1,2), 'ko', 'MarkerFaceColor', 'b'); hold on;
plot(X(c2,1), X(c2,2), 'ko', 'MarkerFaceColor', 'g');hold on;
plot(X(c3,1), X(c3,2), 'ko', 'MarkerFaceColor', 'r')
% Plot the 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', 2)
end
title(sprintf('SVM Linear Classifier with C = %g', 1), 'FontSize', 12)
答案 0 :(得分:0)
您的代码并不接近工作,似乎您似乎缺少许多概念性问题。我会假设你理解:
我仍然认为这里有一个真正的问题。我将采用虹膜数据的两个维度并绘制分离的超平面(在这种情况下为线条)。当您拥有linked code时,您需要做的就是:
这是代码:
S = load('fisheriris');
data = zscore(S.meas);
data = data(:,3:4);
labels = grp2idx(S.species);
opts = '-s 0 -t 2 -c 1 -g 0.25'; %# libsvm training options
indices = crossvalidation(labels, 2);
testIdx = (indices == 1); trainIdx = ~testIdx;
mdl = libsvmtrain_ova(labels(trainIdx), data(trainIdx,:), opts);
figure(1);
numlabels = numel(unique(labels));
testlabels = labels(testIdx);
testdata = data(testIdx,:);
style = {'b+','r+','g+'};
stylel = {'b-','r-','g-'};
for i=1:numlabels,
plot(testdata(find(testlabels==i),1),testdata(find(testlabels==i),2),style{i});
hold on;
w = mdl.models{i}.SVs' * mdl.models{i}.sv_coef;
b = -mdl.models{i}.rho;
x = -2:0.1:2
y = -(w(1)/w(2))*x - (b/w(2));
plot(x,y,stylel{i});
end
grid on;
hold off;
这是情节:
每条彩色线应该将所述颜色的点与所有其他颜色分开。观察这些线是通过训练获得的,而这些点来自我们没有训练的测试数据。