1

I am trying to plot the hyperplane of the SVM from a libsvm. My dataset has 3 features. So referring to the code here I have the following code so far.

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

What I get from that is as follows:

figure

My questions:

  1. Am I doing anything wrong here? Why changes to the code are required to see it properly?

  2. If there are more than 3 features, what is the recommended way to visualize the hyperplane?

Simo Erkinheimo
  • 1,347
  • 9
  • 17
Satinger
  • 123
  • 1
  • 8
  • The code have posted doesn't even attempt to plot the decision boundary. It simply plots the features colored according to their predicted classes. If this is a linear SVM then you can plot a 3D plane as described [here](https://stackoverflow.com/questions/3461869/plot-a-plane-based-on-a-normal-vector-and-a-point-in-matlab-or-matplotlib?noredirect=1&lq=1). For other kernel functions the decision boundary can be sometimes arbitrarily complex (ex. RBF), making it very difficult to create a meaningful 3D visualization. – jodag Jan 09 '18 at 17:33
  • @jodag thanks a lot for the comment. 1) In the link you have given it plots a plane with `surf` calculating z as height relative to x and y pane. How can I just create two panes across the support vectors using that? Please tell me how to do that. 2) So with complex kernels and multiple features it is not possible to visualize the hyperplane? – Satinger Jan 09 '18 at 19:23
  • 1) If the SVM is linear and you want to plot the margins as planes then you need two planes. One where **w**. **x** +b=1 and one where **w**. **x** +b=-1. You would need to convert both to express x3 as a function of x1 and x2. I showed to create such a plane [here](https://stackoverflow.com/questions/47374920) described by **w**. **x** +b=0 which you can modify for your needs. 2) For complex kernels with 3D features its not impossible to visualize the decision boundaries, just not simple like for planes. One method is show the boundary as a voxel rasterization. – jodag Jan 09 '18 at 21:08
  • Trying to visualize a high-dimensional surface isn't usually that helpful in my experience. The math allows one to render projections from higher dimensional space (for example a [4D hypercube](https://en.wikipedia.org/wiki/Hypercube) can be rendered as a projection to 2D) but it's difficult to interpret meaningfully. – jodag Jan 09 '18 at 21:16
  • The answer to [this](https://stackoverflow.com/questions/16146212/how-to-plot-a-hyper-plane-in-3d-for-the-svm-results/19969412) question appears to meet your needs and even supports non-linear SVM models. – jodag Jan 09 '18 at 21:26
  • Thanks a lot. I will check those out. Thanks a lot for your time with the answers, really appreciate. :-) – Satinger Jan 10 '18 at 00:20

0 Answers0