This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money. Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Visualization of linear SVM decision process

We can use the formula in SkLearn to visualize our decision boundaries, support vectors, and two hyperplanes parallel to the decision boundaries.

from sklearn.datasets import make_blobs from sklearn.svm import SVC import matplotlib.pyplot as plt import numpy as np X,y = make_blobs(n_samples=50, centers=2, Random_state = 0, PLT cluster_std = 0.6). The scatter (X [0] :,, [:, 1), X = y c, s = 50, cmap = "rainbow") # rainbow rainbow color PLT. Xticks ([]) plt.yticks([]) plt.show()Copy the code

The decision boundary is W · a+b=0, and two hyperplanes are found on both sides of the decision boundary, so that the relative distance between the hyperplane and the decision boundary is 1. In fact, we only need to connect all the points with a distance of O to the decision boundary on the plane composed of our samples, which is our decision boundary, and connect all the points with a relative distance of 1 to the decision boundary, which is our two hyperplanes parallel to the decision boundary. In this case, our Z is the distance from any point on the plane to the hyperplane.

The second function meshgrid

Xlim = ax.get_xlim() ylim = ax.get_ylim() # Form 30 regular data between maximum and minimum values axisx = np.linspace(xlim[0],xlim[1],30) axisy = np.linspace(ylim[0],ylim[1],30) axisy,axisx = np.meshgrid(axisy,axisx) We will use the two-dimensional array formed here as X and Y in our contour function. Meshgrid function will be used to convert the two one-dimensional vectors into feature matrix. Xy = np.vstack([axisx.ravel(), axyx.ravel ()]).T # where ravel() is a dimension reduction function, Vstack is able to stack rows of uniformly structured one-dimensional arrays #xy is the resulting grid, Scatter (xy[:,0], XY [:,1],s=1,cmap="rainbow") a = Np.array ([1,2,3]) b = Np.array ([7,8]) # how many coordinates do I get? # The answer is six, ,, 7 (1), (2, 7), (3, 7), (1, 8), (2, 8), (3, 8) v1, v2 = np, meshgrid v1 v2 (a, b) v = np. Vstack ([v1. Ravel (), v2, ravel ()]). The T # modeling, CLF = SVC(kernel = "linear").fit(X,y)# ClF.decision_function (xy).shape (0) CLF = SVC(X,y) Decision_function, returns the distance to the decision boundary for each input sample, and then converts this distance to the axisX structure. Scatter (X[:,0],X[:,1],c= Y,s=50,cmap="rainbow") ax = plt.gca() Axisx, axISY,Z,colors="k",levels=[-1,0,1] Z is zero and Z is 1 three lines, alpha = 0.5 # transparency, linestyles = [" -- ", "-", "--"]) ax. Set_xlim (xlim) # set x value ax. Set_ylim (ylim)Copy the code