【问题标题】:Build in function for plotting bayes decision boundary given the probability function在给定概率函数的情况下,构建用于绘制贝叶斯决策边界的函数
【发布时间】:2012-03-17 23:11:27
【问题描述】:

python 中是否有一个函数,如果我们输入一个函数,它会绘制贝叶斯决策边界?我知道matlab中有一个,但我正在寻找python中的一些函数。我知道实现这一点的一种方法是迭代这些点,但我正在寻找一个内置函数。 我在轴上有双变量样本点,我想绘制决策边界以便对它们进行分类。

【问题讨论】:

  • 这样我们就可以找到您要查找的内容,等效的matlab 函数是什么?
  • 我不记得函数的名称了..但几年前我曾经使用过它。也感谢您的尝试

标签: python matlab numpy matplotlib scipy


【解决方案1】:

在上面的 cmets 中猜测 Chris,我假设您想根据高斯混合模型对点进行聚类 - 一种合理的方法,假设基础分布是高斯分布样本的线性组合。下面我展示了一个使用numpy 创建样本数据集的示例,sklearn 用于它的 GM 建模,pylab 用于显示结果。

import numpy as np
from pylab import *
from sklearn import mixture

# Create some sample data
def G(mu, cov, pts):
    return np.random.multivariate_normal(mu,cov,500)

# Three multivariate Gaussians with means and cov listed below
MU  = [[5,3], [0,0], [-2,3]]
COV = [[[4,2],[0,1]], [[1,0],[0,1]], [[1,2],[2,1]]]

A = [G(mu,cov,500) for mu,cov in zip(MU,COV)]
PTS = np.concatenate(A) # Join them together

# Use a Gaussian Mixture model to fit
g = mixture.GMM(n_components=len(A))
g.fit(PTS)

# Returns an index list of which cluster they belong to
C = g.predict(PTS)

# Plot the original points
X,Y = map(array, zip(*PTS))
subplot(211)
scatter(X,Y)

# Plot the points and color according to the cluster
subplot(212)
color_mask = ['k','b','g']
for n in xrange(len(A)):
    idx = (C==n)
    scatter(X[idx],Y[idx],color=color_mask[n])
show()

有关分类方法的更多详细信息,请参阅sklearn.mixture example 页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2020-08-17
    • 1970-01-01
    • 2013-08-17
    • 2020-09-25
    • 2019-12-14
    相关资源
    最近更新 更多