Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. from matplotlib.colors import ListedColormap
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4.  
  5. def plot_decision_regions(X, y, classifier=None, test_idx=None, resolution=0.02):
  6.  
  7. markers = ("s", "x", "o", "^", "v")
  8. colors = ("red", "blue", "lightgreen", "gray", "cyan")
  9. cmap = ListedColormap(colors[:len(np.unique(y))])
  10.  
  11. x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
  12. x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
  13. xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution))
  14.  
  15. Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T).reshape(xx1.shape)
  16. plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
  17. plt.xlim(xx1.min(), xx1.max())
  18. plt.ylim(xx2.min(), xx2.max())
  19.  
  20. for idx, cl in enumerate(np.unique(y)):
  21. plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1], alpha=0.8, c=cmap(idx), marker=markers[idx], label=cl)
  22. if test_idx:
  23. X_test, y_test = X[test_idx[0]:test_idx[1], :], y[test_idx[0]:test_idx[1]]
  24. plt.scatter(X_test[:,0], X_test[:,1], c = "", alpha=1.0, linewidths=1, marker='o', s=55, label="test set")
  25.  
  26. if __name__=="__main__":
  27. plot_decision_regions()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement