问题描述
我想用Python中的三个组件构建和一维绘制一元高斯混合图,其中我已经有其参数,包括mu,sigma,mix factor.
I want to construct and 1D plot a uni-variate Gaussian Mixture with say three components in Python where I already have its parameters including mu,sigma,mix coefficients.
我追求的是在MATLAB中具有等效功能,即gmdistribution(mu,sigma,p)
What I am after has an equivalent in MATLAB i.e. gmdistribution(mu,sigma,p)
我认为代码应如下所示:
I think the code should look sth like this:
from numpy import *
from matplotlib.pylab import *
from sklearn import mixture
gmm = mixture.GMM(n_components=3)
gmm.means_ = np.array([[-1], [0], [3]])
gmm.covars_ = np.array([[1.5], [1], [0.5]]) ** 2
gmm.weights_ = np.array([0.3, 0.5, 0.2])
fig = plt.figure(figsize=(5, 1.7))
ax = fig.add_subplot(131)
#ax.plot(gmm, '-k')
想知道如何做...
欢呼
推荐答案
假设高斯函数是独立的,并且要绘制pdf,则可以只组合按概率加权的基础高斯pdf:
Assuming the Gaussian's are independent, and you want to plot the pdf, you can just combine the underlying Gaussian pdfs weighted by the probabilities:
import numpy as np
import scipy.stats as ss
import matplotlib.pyplot as plt
means = -1., 0., 3.
stdevs = 1.5, 1., 0.5
weights = 0.3, 0.5, 0.2
x = np.arange(-5., 5., 0.01)
pdfs = [p * ss.norm.pdf(x, mu, sd) for mu, sd, p in zip(means, stdevs, weights)]
density = np.sum(np.array(pdfs), axis=0)
plt.plot(x, density)
这是正确的,需要一点基本概率论.
That this is correct requires a little elementary probability theory.
这篇关于如何在Python中使用其参数构造和绘制单变量高斯混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!