(model, resolution=100, colors=('b', 'k', 'r'))
| 116 | |
| 117 | |
| 118 | def plot_decision_boundary(model, resolution=100, colors=('b', 'k', 'r')): |
| 119 | np.warnings.filterwarnings('ignore') |
| 120 | fig, ax = plt.subplots() |
| 121 | |
| 122 | # Generate coordinate grid of shape [resolution x resolution] |
| 123 | # and evaluate the model over the entire space |
| 124 | x_range = np.linspace(model.Xtrain[:,0].min(), model.Xtrain[:,0].max(), resolution) |
| 125 | y_range = np.linspace(model.Xtrain[:,1].min(), model.Xtrain[:,1].max(), resolution) |
| 126 | grid = [[model._decision_function(np.array([[xr, yr]])) for yr in y_range] for xr in x_range] |
| 127 | grid = np.array(grid).reshape(len(x_range), len(y_range)) |
| 128 | |
| 129 | # Plot decision contours using grid and |
| 130 | # make a scatter plot of training data |
| 131 | ax.contour(x_range, y_range, grid.T, (-1, 0, 1), linewidths=(1, 1, 1), |
| 132 | linestyles=('--', '-', '--'), colors=colors) |
| 133 | ax.scatter(model.Xtrain[:,0], model.Xtrain[:,1], |
| 134 | c=model.Ytrain, lw=0, alpha=0.3, cmap='seismic') |
| 135 | |
| 136 | # Plot support vectors (non-zero alphas) |
| 137 | # as circled points (linewidth > 0) |
| 138 | mask = model.alphas > 0. |
| 139 | ax.scatter(model.Xtrain[:,0][mask], model.Xtrain[:,1][mask], |
| 140 | c=model.Ytrain[mask], cmap='seismic') |
| 141 | |
| 142 | # debug |
| 143 | ax.scatter([0], [0], c='black', marker='x') |
| 144 | |
| 145 | plt.show() |
no test coverage detected