MATLAB中的SVM可视化 [英] SVM Visualization in MATLAB

查看:796
本文介绍了MATLAB中的SVM可视化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中执行SVM训练后,如何可视化SVM分类?

How do I visualize the SVM classification once I perform SVM training in Matlab?

到目前为止,我仅使用以下方法训练了SVM:

So far, I have only trained the SVM with:

% Labels are -1 or 1
groundTruth = Ytrain;
d = xtrain;

model = svmtrain(groundTruth, d);

推荐答案

如果使用LIBSVM,则可以绘制分类结果:

If you are using LIBSVM, you can plot classification results:

% Labels are -1 or 1
groundTruth = Ytrain;
d = xtrain;

figure

% plot training data
hold on;
pos = find(groundTruth==1);
scatter(d(pos,1), d(pos,2), 'r')
pos = find(groundTruth==-1);
scatter(d(pos,1), d(pos,2), 'b')

% now plot support vectors
hold on;
sv = full(model.SVs);
plot(sv(:,1),sv(:,2),'ko');

% now plot decision area
[xi,yi] = meshgrid([min(d(:,1)):0.01:max(d(:,1))],[min(d(:,2)):0.01:max(d(:,2))]);
dd = [xi(:),yi(:)];
tic;[predicted_label, accuracy, decision_values] = svmpredict(zeros(size(dd,1),1), dd, model);toc
pos = find(predicted_label==1);
hold on;
redcolor = [1 0.8 0.8];
bluecolor = [0.8 0.8 1];
h1 = plot(dd(pos,1),dd(pos,2),'s','color',redcolor,'MarkerSize',10,'MarkerEdgeColor',redcolor,'MarkerFaceColor',redcolor);
pos = find(predicted_label==-1);
hold on;
h2 = plot(dd(pos,1),dd(pos,2),'s','color',bluecolor,'MarkerSize',10,'MarkerEdgeColor',bluecolor,'MarkerFaceColor',bluecolor);
uistack(h1, 'bottom');
uistack(h2, 'bottom');

这篇关于MATLAB中的SVM可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆