python 画出决策边界_python - 使用Matplotlib的pyplot绘制将2个类分开的决策边界

我真的可以使用技巧来帮助我绘制决策边界以区分数据类别。我通过Python NumPy创建了一些样本数据(来自高斯分布)。在这种情况下,每个数据点都是2D坐标,即由2行组成的1列 vector 。例如。,[ 1

2 ]

假设我有2个类,class1和class2,并通过下面的代码(分配给变量x1_samples和x2_samples)为class1创建了100个数据点,为class2创建了100个数据点。

mu_vec1 = np.array([0,0])

cov_mat1 = np.array([[2,0],[0,2]])

x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1, 100)

mu_vec1 = mu_vec1.reshape(1,2).T # to 1-col vector

mu_vec2 = np.array([1,2])

cov_mat2 = np.array([[1,0],[0,1]])

x2_samples = np.random.multivariate_normal(mu_vec2, cov_mat2, 100)

mu_vec2 = mu_vec2.reshape(1,2).T

当我绘制每个类的数据点时,它看起来像这样:

现在,我想出了一个决策边界方程,将两个类别分开,并希望将其添加到绘图中。但是,我不确定如何绘制此函数:

def decision_boundary(x_vec, mu_vec1, mu_vec2):

g1 = (x_vec-mu_vec1).T.dot((x_vec-mu_vec1))

g2 = 2*( (x_vec-mu_vec2).T.dot((x_vec-mu_vec2)) )

return g1 - g2

我将非常感谢您的帮助!

编辑:

直观地(如果我做的数学正确),当我绘制函数时,我希望决策边界看起来像这条红线...

最佳答案

您的问题比简单的绘图还要复杂:您需要绘制轮廓,以最大程度地提高类间距离。幸运的是,这是一个经过充分研究的领域,特别是对于SVM机器学习。

最简单的方法是下载scikit-learn模块,该模块提供了许多很酷的方法来划定边界:scikit-learn : Support Vector Machines

代码:# -*- coding: utf-8 -*-

import numpy as np

import matplotlib

from matplotlib import pyplot as plt

import scipy

from sklearn import svm

mu_vec1 = np.array([0,0])

cov_mat1 = np.array([[2,0],[0,2]])

x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1, 100)

mu_vec1 = mu_vec1.reshape(1,2).T # to 1-col vector

mu_vec2 = np.array([1,2])

cov_mat2 = np.array([[1,0],[0,1]])

x2_samples = np.random.multivariate_normal(mu_vec2, cov_mat2, 100)

mu_vec2 = mu_vec2.reshape(1,2).T

fig = plt.figure()

plt.scatter(x1_samples[:,0],x1_samples[:,1], marker='+')

plt.scatter(x2_samples[:,0],x2_samples[:,1], c= 'green', marker='o')

X = np.concatenate((x1_samples,x2_samples), axis = 0)

Y = np.array([0]*100 + [1]*100)

C = 1.0 # SVM regularization parameter

clf = svm.SVC(kernel = 'linear', gamma=0.7, C=C )

clf.fit(X, Y) Linear Plot

w = clf.coef_[0]

a = -w[0] / w[1]

xx = np.linspace(-5, 5)

yy = a * xx - (clf.intercept_[0]) / w[1]

plt.plot(xx, yy, 'k-')

MultiLinear Plot

C = 1.0 # SVM regularization parameter

clf = svm.SVC(kernel = 'rbf', gamma=0.7, C=C )

clf.fit(X, Y)

h = .02 # step size in the mesh

# create a mesh to plot in

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1

y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1

xx, yy = np.meshgrid(np.arange(x_min, x_max, h),

np.arange(y_min, y_max, h))

# Plot the decision boundary. For that, we will assign a color to each

# point in the mesh [x_min, m_max]x[y_min, y_max].

Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot

Z = Z.reshape(xx.shape)

plt.contour(xx, yy, Z, cmap=plt.cm.Paired)

实作

如果要自己实现,则需要解决以下二次方程式:

维基百科文章

不幸的是,对于像您绘制的那样的非线性边界,依靠内核技巧是一个困难的问题,但是没有明确的解决方案。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值