Both linear models have linear decision boundaries (intersecting hyperplanes)
while the non-linear kernel models (polynomial or Gaussian RBF) have more
flexible non-linear decision boundaries with shapes that depend on the kind of
kernel and its parameters.

Note

while plotting the decision function of classifiers for toy 2D
datasets can help get an intuitive understanding of their respective
expressive power, be aware that those intuitions don’t always generalize to
more realistic high-dimensional problems.

print(__doc__)importnumpyasnpimportmatplotlib.pyplotaspltfromsklearnimportsvm,datasets# import some data to play withiris=datasets.load_iris()X=iris.data[:,:2]# we only take the first two features. We could# avoid this ugly slicing by using a two-dim datasety=iris.targeth=.02# step size in the mesh# we create an instance of SVM and fit out data. We do not scale our# data since we want to plot the support vectorsC=1.0# SVM regularization parametersvc=svm.SVC(kernel='linear',C=C).fit(X,y)rbf_svc=svm.SVC(kernel='rbf',gamma=0.7,C=C).fit(X,y)poly_svc=svm.SVC(kernel='poly',degree=3,C=C).fit(X,y)lin_svc=svm.LinearSVC(C=C).fit(X,y)# create a mesh to plot inx_min,x_max=X[:,0].min()-1,X[:,0].max()+1y_min,y_max=X[:,1].min()-1,X[:,1].max()+1xx,yy=np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h))# title for the plotstitles=['SVC with linear kernel','LinearSVC (linear kernel)','SVC with RBF kernel','SVC with polynomial (degree 3) kernel']fori,clfinenumerate((svc,lin_svc,rbf_svc,poly_svc)):# Plot the decision boundary. For that, we will assign a color to each# point in the mesh [x_min, x_max]x[y_min, y_max].plt.subplot(2,2,i+1)plt.subplots_adjust(wspace=0.4,hspace=0.4)Z=clf.predict(np.c_[xx.ravel(),yy.ravel()])# Put the result into a color plotZ=Z.reshape(xx.shape)plt.contourf(xx,yy,Z,cmap=plt.cm.coolwarm,alpha=0.8)# Plot also the training pointsplt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.coolwarm)plt.xlabel('Sepal length')plt.ylabel('Sepal width')plt.xlim(xx.min(),xx.max())plt.ylim(yy.min(),yy.max())plt.xticks(())plt.yticks(())plt.title(titles[i])plt.show()