1

I'm trying to plot decision boundaries of SVM with different kernels like rbf, poly, and linear.

I am using iris data set available online which is in shape of 150 * 4, so I had dropped the 4th feature and now it's in shape of 150 * 3 . Notice that each class now contains 50 samples with 3 features in order of their appearances.

class1 = iris[:50, :], class2 = iris[50:100, :], class3 = iris[100:150, :]

I have already plotted the one with 'linear' kernel BUT I have no idea about how to plot with other kernels. I've searched for days and didn't found anything that I would understand or I could use.

These are two surfaces which separates different classes

    z_linear = lambda x, y: (-clf.intercept_[0] - clf.coef_[0][0] * x - clf.coef_[0][1] * y) / clf.coef_[0][2]
    w_linear = lambda x, y: (-clf.intercept_[2] - clf.coef_[2][0] * x - clf.coef_[2][1] * y) / clf.coef_[2][2]

Decision boundaries of SVM with linear kernel

Now I need to plot 3 classes and the surfaces that separates them by using other kernels (i.e 'rbf', 'poly' with 'degree=3')

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Sadegh
  • 125
  • 11

2 Answers2

0

I guess what you should do, is plot a non linear line separating these points. This is what RBF/poly kernel essentially do, they find the non linear hyperplane separating the classes

Follow these 2 links: https://scikit-learn.org/0.18/auto_examples/svm/plot_iris.html

https://jakevdp.github.io/PythonDataScienceHandbook/05.07-support-vector-machines.html

Chandan Malla
  • 481
  • 5
  • 14
  • These solutions can only be used in 2 dimensional features and I couldn't customize them to a 3-D feature. When I follow the implementations usually there is dimension problem like `xx, yy, zz = meshgrid(X, Y, Z)` is not in the dimension which I want. I didn't understand it's problem. – Sadegh Jan 19 '21 at 17:11
0
def draw_line(coef,intercept, mi, ma):
    # for the separating hyper plane ax+by+c=0, the weights are [a, b] and the intercept is c
    # to draw the hyper plane we are creating two points
    # 1. ((b*min-c)/a, min) i.e ax+by+c=0 ==> ax = (-by-c) ==> x = (-by-c)/a here in place of y we are keeping the minimum value of y
    # 2. ((b*max-c)/a, max) i.e ax+by+c=0 ==> ax = (-by-c) ==> x = (-by-c)/a here in place of y we are keeping the maximum value of y
    points=np.array([[((-coef[1]*mi - intercept)/coef[0]), mi],[((-coef[1]*ma - intercept)/coef[0]), ma]])
    plt.plot(points[:,0], points[:,1])



def svm_margin(c):
    ratios = [(100,2), (100, 20), (100, 40), (100, 80)]
    plt.figure(figsize=(20,5))
    for j,i in enumerate(ratios):
        plt.subplot(1, 4, j+1)
        X_p=np.random.normal(0,0.05,size=(i[0],2))
        X_n=np.random.normal(0.13,0.02,size=(i[1],2))
       
        y_p=np.array([1]*i[0]).reshape(-1,1)
        y_n=np.array([0]*i[1]).reshape(-1,1)
        
        X=np.vstack((X_p,X_n))
        y=np.vstack((y_p,y_n))
        
        plt.scatter(X_p[:,0],X_p[:,1],color='yellow')
        plt.scatter(X_n[:,0],X_n[:,1],color='red')
    
        ###SVM
        clf = SVC(kernel='linear',C=c)
        clf.fit(X,y)
        coefficient = clf.coef_[0]
        intercept = clf.intercept_
        margin = 1 / (np.sqrt(np.sum(clf.coef_ ** 2)))
        draw_line(coefficient,intercept,min(X[:,1]),max(X[:,1]))
        ### Intercept for parallel hyper place is (intercept +/- 1)
        draw_line(coefficient,intercept - margin * np.sqrt(np.sum(clf.coef_ ** 2)) ,min(X[:,1]),max(X[:,1]))
        draw_line(coefficient,intercept + margin * np.sqrt(np.sum(clf.coef_ ** 2)) ,min(X[:,1]),max(X[:,1]))
        ###https://scikit-learn.org/stable/auto_examples/svm/plot_svm_margin.html
        plt.scatter(X[clf.support_][:,0],X[clf.support_][:,1],facecolors='none',edgecolors='k')
    plt.suptitle('SVM Margin Hyperplane For C = ' + str(c))
    plt.show()
    
svm_margin(0.001)
svm_margin(1)
svm_margin(100)

Try and extend this to a 3-dimensional system, Output produced by above code for reference:

enter image description here

Chandan Malla
  • 481
  • 5
  • 14