def plot_svc_decision_function(model, ax=None, plot_support=True):
"""Plot the decision function for a 2D SVC"""
if ax is None:
ax = plt.subplot(111)
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# create grid to evaluate model
x = np.linspace(xlim[0], xlim[1], 30)
y = np.linspace(ylim[0], ylim[1], 30)
X,Y = np.meshgrid(x, y)
xy = np.vstack([X.flatten(), Y.flatten()]).T
P = model.decision_function(xy).reshape(X.shape)
# plot decision boundary and margins
#levels是 alpha是透明度 linestyles
ax.contour(X, Y, P, colors='k',
levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'])
# plot support vectors
if plot_support:
ax.scatter(model.support_vectors_[:, 0],
model.support_vectors_[:, 1],
s=500,c='',edgecolors='black')