![](/img/trans.png)
[英]python Fitting weighted data with Gaussian mixture model (GMM) with minimum on covariance
[英]Python multi-gaussian Fitting - ValueError: GMM estimation with 2 components, but got only 1 samples
我有兩個要擬合的高斯分布。 由於兩個發行版可以不同地混合使用,因此我希望擬合度盡可能通用。 我在這里找到以下代碼:
高斯擬合到python中的直方圖數據:Trust Region v / s Levenberg Marquardt-第一個答案。
但是,它不適用於我的數據或下面代碼中生成的原始數據,並吐出錯誤:
ValueError: GMM estimation with 2 components, but got only 1 samples
我希望它簡單一些。 我的數據只是一個2D數組,可繪制直方圖,時間與幅度的關系。
import numpy as np
from sklearn import mixture
import matplotlib.pyplot as plt
comp0 = np.random.randn(1000) - 5 # samples of the 1st component
comp1 = np.random.randn(1000) + 5 # samples of the 2nd component
x = np.hstack((comp0, comp1)) # merge them
gmm = mixture.GMM(n_components=2) # gmm for two components
gmm.fit(x) # train it!
linspace = np.linspace(-10, 10, 1000)
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.hist(x, 100) # draw samples
ax2.plot(linspace, np.exp(gmm.score_samples(linspace)[0]), 'r')
plt.show()
采用:
x = np.vstack((comp0, comp1))
代替hstack
因為每一行應表示一個樣本,而每一列應表示一個樣本特征。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.