3

I am working with Python/Pandas and I'm trying to plot a decision boundary. According to [1] and [2], this is considered best practice:

# X - some data in 2dimensional np.array

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))

# here "model" is your model's prediction (classification) function
Z = model(np.c_[xx.ravel(), yy.ravel()]) 

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=pl.cm.Paired)
plt.axis('off')

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired)

My question concerns this line: Z = model(np.c_[xx.ravel(), yy.ravel()])

My model function currently accepts a feature Series as input (representing a single datapoint). I need to modify it so that it can accept a mesh grid and return a numpy array with predictions for all points on the grid. How can I do this efficiently? I am not familiar with NumPy data types and I'm not good at vectorizing operations.

Community
  • 1
  • 1
Atte Juvonen
  • 4,922
  • 7
  • 46
  • 89

0 Answers0