2

I am using scikit-learn to understand Support Vector Machines(SVM). I want to plot the decision boundary computed by SVM. The SVM uses 3 features. So the decision boundary must be drawn in 3D space. Is this possible using scikit-learn? I could find only 2D plots of SVM decision boundary at the official website. However I found links on stackoverflow which show this is possible using matlab and r. Is there a way in which I can achieve the same using scikit-learn?

1
  • When you get to a resolution, please remember to up-vote useful things and accept your favourite answer (even if you have to write it yourself), so Stack Overflow can properly archive the question.
    – Prune
    Commented Apr 9, 2018 at 16:50

1 Answer 1

4

You don't use scikit-learn to plot things in Python. You have to use another package, and if you check scikit-learn's examples, you will see they use matplotlib. You can create 3D plots with matplotlib, here is the tutorial. The usage is basically the same than in 2D, except you add an argument for the third dimension.

Another popular package you could use is mayavi, which was conceived especially for 3D plotting.

3
  • Yes I accept that for 3D plot I have to use matplotlib. However my question was can I plot the decision boundary of the SVM when using 3 features? In the examples shown, they show the usage of 2D plot(because they use 2 features) for showing the decision boundary. I could not find an example where they use 3 features and show the decision boundary.
    – AviB
    Commented Apr 29, 2015 at 8:58
  • 1
    I am not sure I understand your problem. Take this example (scikit-learn.org/stable/auto_examples/svm/plot_iris.html). If you have 3 features, all you have to do is add a z coordinate to your meshgrid defined similarly as x and y and pass it to your predict method and contourf method. Commented Apr 29, 2015 at 10:56
  • Great! This is what I wanted to know; how to add the z coordinate to the plot. I am going to try it.
    – AviB
    Commented Apr 30, 2015 at 4:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.