MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / plot_decision_boundary

Function plot_decision_boundary

svm_class/util.py:118–145  ·  view source on GitHub ↗
(model, resolution=100, colors=('b', 'k', 'r'))

Source from the content-addressed store, hash-verified

116
117
118def 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()

Callers 2

svm_smo.pyFile · 0.90
svm_gradient.pyFile · 0.90

Calls 1

_decision_functionMethod · 0.45

Tested by

no test coverage detected