【发布时间】:2015-08-11 05:57:20
【问题描述】:
在 scikit-learn 中,使用 GMM 拟合高斯峰似乎适用于离散数据数据点。 有没有一种方法可以将 GMM 与已经分箱或聚合成直方图的数据一起使用?
例如,以下代码是一种变通方法,可在拟合之前将分箱数据转换为离散数据点:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import mixture
def fit_one_peak(x, linspace):
gmm = mixture.GMM(n_components=1) # gmm for one components
gmm.fit(x) # train it!
m1 = gmm.means_
w1 = gmm.weights_
return np.exp(gmm.score_samples(linspace)[0]), m1[0][0], w1[0]
def convert_to_signal(d, s):
c = []
count = 0
for i in s:
for j in range(int(d[count])): # No floats!
c.append(i)
count += 1
return c
d = [0.5, 2, 5, 3, 1, 0.5] # y data, which is already binned
s = [0, 1, 2, 3, 4, 5] # x data
signal = convert_to_signal(d, s)
linspace = np.linspace(s[0], s[-1], len(s))
l, mean, weight = fit_one_peak(signal, linspace)
l = l*(np.max(d)/ np.max(l)) # Normalize the fitted y
fig = plt.figure()
plt.plot(s, d, label='Original')
plt.plot(linspace, l, label='Fitted')
plt.hist(signal, label='Re-binned')
plt.legend()
【问题讨论】:
标签: python scikit-learn