0

I plot a contour plot which indicates the seperating hyperplane of a SVC estimator in a 2D axes using the following code.

X,y= make_circles(n_samples=50,factor=.1,noise=.1)

x_fit=np.linspace(-1.5,1.5,10)
y_fit=np.linspace(-1.5,1.5,10)
Y,XX=np.meshgrid(x_fit,y_fit)

xy=np.vstack([XX.ravel(),Y.ravel()]).T

P=clf.decision_function(xy).reshape(XX.shape)
plt.contour(XX,Y,P,colors="k",levels=[-1,0,1],alpha=0.5,linestyles=["--","-","--"])

enter image description here

Question

Based on this question and the answer of Ilya V. Schurov there is still one issue for me. I understand, that X and Y provides the x and y values and Z provides the "depth" for each xy coordiante and thus has to be 2 dimensional. Further, the X and Y values of the plt.contour() function can be either 1D or 2D (if 1D the meshgrid gets computed internally).

BUT what is the benefit/ reason for X and Y to be 2D? Because actually the "second dimension" of X and Y can not be plotted on a 2D axes. So has it some "algorithmic performance" reasons for X and Y to be 2D or what is the reason?

2Obe
  • 3,570
  • 6
  • 30
  • 54
  • 1
    `XX` and `Y` do not have to be 2D. They can be 1D as well. However, `P` must be 2D. Look at the [duplicate question](https://stackoverflow.com/questions/42045921/why-does-pyplot-contour-require-z-to-be-a-2d-array) as well as the [contour documentation](http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.contour.html#matplotlib-axes-axes-contour). If this does not help you, please edit your question to make it sufficiently clear at what point you have problems to understand the matter. – ImportanceOfBeingErnest Jul 16 '17 at 22:03
  • Thanks! Youre right and I will specify my question – 2Obe Jul 17 '17 at 02:06
  • 2
    So if `X` and `Y` are unidimensional and `Z` is two dimensional, how would you get the x and y coordinates that correspond to the value `Z[i,j]`? Isn't the best and easiest way to have `X` and `Y` being 2D as well, such that the coordinates of `Z[i,j]` are simply `X[i,j]` and `Y[i,j]`? – ImportanceOfBeingErnest Jul 18 '17 at 13:12
  • Thanks you are right. Now I got it :) – 2Obe Jul 18 '17 at 13:16

1 Answers1

1

Contour plot is not designed for just plotting hyperplanes for classfier. It represents a 3-D surface with a 2-D format; or it plots elevations of a 2-D area. Therefore, plt.contour() has to somehow understand/know elevations covering the whole area. One way, or the current way, is to provide a set of elevations for a set of points covering the 2-D area. And the more you provide, the better/finer the final contour plot is. When providing a 1-D x and y, it represents a line rather than an area, which cannot be used to interpolated a 2-D area.

Another way to plot hyperplanes is to calculate the exact planes yourself. Then you can plot hyperplanes with a 1-D linespace. But I don't think this will be easier than using plt.contour() since plt.contour() did the hard calculation by simulating with interpolation for you.

Edit: How Z works with X and Y in plt.contour()?

It takes some assumption for Z works with X and Y.

  • If X and Y is 2-D, a value in Z is the depth for a point specified by corresponding (same location by index) values in X and Y.
  • If X and Y is 1-D, it will be convert to a meshgrid first, as you can see in the source code. Then the rest will work the same way as explained above.

So for your case specifically, using x_fit and y_fit can give you the same result because plt.contour() makes the meshgrid for you. As long as you understand the mechanism, either way is fine. The only thing I would say is if you end up making the meshgrid for calculating P anyway, why not using the meshgrid to avoid assumption/ambiguity?

Y. Luo
  • 5,622
  • 1
  • 18
  • 25
  • Thank you. But isnt it that way, that the plt.contour() function wants three attributes, the x values, the y values and the z values which represent the x values, the y values and the depth? Thus isn't it sufficient for x and y to be 1 dimensional like x_fit and y_fit? What I dont understand is, WHY XX and Y should be 2 dimensional and for what this 2 dimensional Data is used because on a 2 dimensional axes I cant plot 2D XX and 2D Y values....can you please eyplain this to me? – 2Obe Jul 16 '17 at 19:42
  • @2Obe I don't think "z values which represent the x values, the y values and the depth". `Z` only has values for depth. I updated my answer with explanation about how `Z` work with `X` and `Y`. Please check if you have time. – Y. Luo Jul 17 '17 at 05:15
  • Have specified my question – 2Obe Jul 18 '17 at 09:03