简体   繁体   English

非线性决策边界支持向量机

[英]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)我有 2 个带有数值数据的特征,我做了一个简单的线性决策边界(见下图)我现在的决策边界

Now the thing is that I would like my red line to look like the black line:现在问题是我希望我的红线看起来像黑线:我想要什么

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. model 是 SVM,我执行了 GridSearchCV 并得到了最佳估计器。 I used a linear kernel to be able to get the models coefs and intercept.我使用线性 kernel 来获取模型系数和截距。

I can add a third dimension to the equation if needed.如果需要,我可以在等式中添加第三个维度。 I have plenty of columns available in my df.我的 df 中有很多可用的列。 I only kept the 2 most important ones (I got those from looking the model's feature importance).我只保留了 2 个最重要的(我是通过查看模型的特征重要性得到的)。

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.最好的事情是,如果我可以有一个方程来绘制决策边界,并且可以包含在我的数据集中,并且可以用作“制裁”,就像方程制裁的结果高于 0 一样,样本的目标为 1,否则为 0。

Like this (something I made with another model but for 3D plot):像这样(我用另一个 model 制作的东西,但用于 3D 情节):

# 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))

Support-Vector-Machines-SVM-/Kernel Trick SVM.ipynb 支持向量机-SVM-/内核技巧 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM