8

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

So far, I have only trained the SVM with:

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

model = svmtrain(groundTruth, d);
Wok
  • 4,956
  • 7
  • 42
  • 64
djpark121
  • 1,191
  • 3
  • 10
  • 5

4 Answers4

7

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');
Wok
  • 4,956
  • 7
  • 42
  • 64
Lordalcol
  • 990
  • 7
  • 22
2

Assuming your data has more than two dimensions, you can perform a PCA, project the data to 2D, then assign them a color according to the output of your svm classifier (e.g., red for class A, blue for class B). This is quick to do and you will see if there is anything to visualize. However, data with a high dimensionality are not likely to be easily visualizable in 2D.

levesque
  • 8,756
  • 10
  • 36
  • 44
  • What do you mean by _performing a PCA_? Haw can I do that? Would you explain a bit more please? I want to visualize the svm classification on FisherIris data set(using libsvm).If I'm right it's 4D – Zahra E Dec 28 '12 at 15:58
  • To be honest, if you don't even know what a PCA is (short for principal component analysis), you would probably end up spending too much time for the end result. It's not trivial. I understand it would be interesting to visualize the decision boundary, but you might want to consider different strategies to analyze the behavior of your SVMs. What exactly are you trying to achieve? – levesque Jan 07 '13 at 16:42
  • Finally I decided to use the [stprtool](http://cmp.felk.cvut.cz/cmp/software/stprtool/) instead of libsvm to get the choice of plotting the decision boundary. It worked really fine. Thank you for your response – Zahra E Jan 08 '13 at 05:59
0

Check out this svm-toy function similar to the one in LIBSVM. Obviously it only works for 2D binary classification

Amro
  • 123,847
  • 25
  • 243
  • 454
-2
model = svmtrain(groundTruth, d, 'ShowPlot', true);
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Wang
  • 1