首页 > 解决方案 > 具有离散数据的高斯混合模型

问题描述

我有 136 个数字,它们具有 8 个高斯分布的重叠分布。我想找到它的平均值,以及每个高斯分布的方差!你能发现我的代码有什么错误吗?

file = open("1.txt",'r') #data is in 1.txt like 0,0,0,0,0,0,1,0,0,1,4,4,6,14,25,43,71,93,123,194...

y=[int (i) for i in list((file.read()).split(','))] # I want to make list which element is above data

x=list(range(1,len(y)+1)) # it is x values

z=list(zip(x,y)) # z elements consist as (1, 0), (2, 0), ...

因此,通过上述过程,对于以第一个给定数据为y值的xy平面上的136个点(x,y),得到了以此为元素的列表z。现在我想获得每个高斯分布的均值,方差。此时,基本假设是给定数据由重叠的 8 个高斯分布组成。

import numpy as np

from sklearn.mixture import GaussianMixture

data = np.array(z).reshape(-1,1)

model = GaussianMixture(n_components=8).fit(data)

print(model.means_)

file.close()

实际上,我不知道如何制作代码来打印 8 种均值和方差...任何人都可以帮助我吗?

标签: pythonnumpygaussian

解决方案


您可以使用它,我为您的可视化制作了示例代码 -

import numpy as np
from sklearn.mixture import GaussianMixture
import scipy
import matplotlib.pyplot as plt
%matplotlib inline

#Sample data
x = [0,0,0,0,0,0,1,0,0,1,4,4,6,14,25,43,71,93,123,194]
num_components = 2

#Fit a model onto the data
data = np.array(x).reshape(-1,1)
model = GaussianMixture(n_components=num_components).fit(data)

#Get list of means and variances
mu = np.abs(model.means_.flatten())
sd = np.sqrt(np.abs(model.covariances_.flatten()))

#Plotting
extend_window = 50  #this is for zooming into or out of the graph, higher it is , more zoom out
x_values = np.arange(data.min()-extend_window, data.max()+extend_window, 0.1) #For plotting smooth graphs
plt.plot(data, np.zeros(data.shape), linestyle='None', markersize = 10.0, marker='o') #plot the data on x axis

#plot the different distributions (in this case 2 of them)
for i in range(num_components):
    y_values = scipy.stats.norm(mu[i], sd[i])
    plt.plot(x_values, y_values.pdf(x_values))

在此处输入图像描述


推荐阅读