How to use meshgrid with large arrays in Matplotlib?

1.4k views Asked by At

I have trained a machine learning binary classifier on a 100x85 array in sklearn. I would like to be able to vary 2 of the features in the array, say column 0 and column 1, and generate contour or surface graph, showing how the predicted probability of falling in one category varies across the surface.

It seems reasonable to me that I would use something like the following:

X = 100 x 85 array of data used for training set clf = Trained 2-class classifier

x = np.array(X)
y = np.array(X)

x[:,0] = np.linspace(0, 100, 100)
y[:,1] = np.linspace(0, 100, 100)
xx, yy = meshgrid(x,y)

The next step would be to use

clf.predict_proba(<input arrays>)

followed by plotting, but using meshgrid results in two 8500x8500 matrices that can't be used in my classifier.

How do I get the necessary 100x85 vector at each point in the grid to use pred_proba with my classifier?

Thanks for any help you can provide.

1

There are 1 answers

2
tmdavison On BEST ANSWER

As @wflynny says above, you need to give np.meshgrid two 1D arrays. We can use X.shape to create your x and y arrays, like this:

X=np.zeros((100,85)) # just to get the right shape here

print X.shape
# (100, 85)

x=np.arange(X.shape[0])
y=np.arange(X.shape[1])

print x.shape
# (100,)
print y.shape
# (85,)

xx,yy=np.meshgrid(x,y,indexing='ij')

print xx.shape
#(100, 85)
print yy.shape
#(100, 85)