'Non Linear Decision boundary SVM

I need you guys help to find a non linear decision boundary. I have 2 features with numerical data, I made a simple linear decision boundary (see picture below) The decision boundary I have at the moment

Now the thing is that I would like my red line to look like the black line: What I would like to have

the 'equation' I used for plotting the red line is:

# x and y not related to the dataset, those variables are only for plotting the red line
# mdl is my model

x = np.linspace(0.6, -0.6) 
y = -(mdl.coef_[0][0] / mdl.coef_[0][1]) * x - mdl.intercept_ / mdl.coef_[0][1]

The model is a SVM, I performed a GridSearchCV and got the best estimator. I used a linear kernel to be able to get the models coefs and intercept.

I can add a third dimension to the equation if needed. I have plenty of columns available in my df. I only kept the 2 most important ones (I got those from looking the model's feature importance).

The best thing would be if I could have an equation for plotting the decision boundary and one that I could include in my dataset, and that would be used as a 'sanction', like if the result of the equation sanction is above 0, sample's target is 1, else it's 0.

Like this (something I made with another model but for 3D plot):

# Equation sanction that goes into my dataset
df['sanction'] = df.widthS63R04 * model.coef_[0][0] + df.chordS67R04 * model.coef_[0][1] + df.chordS71R04 * model.coef_[0][2]

#Equation for 3D Hyperplane
tmp = np.linspace(-5,5,30)
x,y = np.meshgrid(tmp,tmp)
z = lambda x,y: (-mdl.intercept_[0]-mdl.coef_[0][0]*x -mdl.coef_[0][1]*y) / mdl.coef_[0][2]
# lambda usage for 3d surface hyperplane
ax.plot_surface(x, y, z(x, y))


Solution 1:[1]

Support-Vector-Machines-SVM-/Kernel Trick SVM.ipynb

zero_one_colourmap = ListedColormap(('blue', 'red'))
def plot_decision_boundary(X, y, clf):
    X_set, y_set = X, y
    X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, 
                             stop = X_set[:, 0].max() + 1, 
                             step = 0.01),
                   np.arange(start = X_set[:, 1].min() - 1, 
                             stop = X_set[:, 1].max() + 1, 
                             step = 0.01))

plt.contourf(X1, X2, clf.predict(np.array([X1.ravel(), 
                                         X2.ravel()]).T).reshape(X1.shape),
           alpha = 0.75, 
           cmap = zero_one_colourmap)
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
            c = (zero_one_colourmap)(i), label = j)
plt.title('SVM Decision Boundary')
plt.xlabel('X1')
plt.ylabel('X2')
plt.legend()
return plt.show()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1