Skip to content

Instantly share code, notes, and snippets.

@dennybritz
Created September 18, 2015 16:45
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save dennybritz/ff8e7c2954dd47a4ce5f to your computer and use it in GitHub Desktop.
Save dennybritz/ff8e7c2954dd47a4ce5f to your computer and use it in GitHub Desktop.
plot_decision_boundary.py
# Helper function to plot a decision boundary.
# If you don't fully understand this function don't worry, it just generates the contour plot below.
def plot_decision_boundary(pred_func):
# Set min and max values and give it some padding
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
h = 0.01
# Generate a grid of points with distance h between them
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# Predict the function value for the whole gid
Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# Plot the contour and training examples
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
@LRDPRDX
Copy link

LRDPRDX commented Mar 3, 2017

There is error because of the last line: Unexpected indent.

@hack-r
Copy link

hack-r commented Aug 25, 2017

@BogdanBessit The error is in how you pasted his code, not in the function...

@duhaime
Copy link

duhaime commented Dec 21, 2017

This code comes more or less from the Scikit docs, e.g. in their example of a KNN classifier

@DiWuDi
Copy link

DiWuDi commented Oct 1, 2018

Just click the "raw" on the top right corner, and copy from there. Then past it to your Jupyter cell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment