def PolynomialSVC(degree, C=1.0): return Pipeline([ ("poly", PolynomialFeatures(degree=degree)), ("std_scaler", StandardScaler()), ("linearSVC", LinearSVC(C=C)) ])
ploy_svc = PolynomialSVC(degree=3) ploy_svc.fit(X, y)
def plot_decision_boundary(model, axis): X0, X1 = np.meshgrid( np.linspace(axis[0], axis[1], int((axis[1] - axis[0]) * 100)).reshape(-1, 1), np.linspace(axis[2], axis[3], int((axis[3] - axis[2]) * 100)).reshape(-1, 1), ) X_grid_matrix = np.c_[X0.ravel(), X1.ravel()] y_predict = model.predict(X_grid_matrix) y_predict_matrix = y_predict.reshape(X0.shape) from matplotlib.colors import ListedColormap my_colormap = ListedColormap(['#0000CD', '#40E0D0', '#FFFF00']) plt.contourf(X0, X1, y_predict_matrix, linewidth=5, cmap=my_colormap)
plot_decision_boundary(ploy_svc, axis=[-1.5, 2.5, -1.0, 1.5]) plt.scatter(X[y==0, 0], X[y==0, 1]) plt.scatter(X[y==1, 0], X[y==1, 1]) plt.show()
|