簡體   English   中英

具有離散數據的高斯混合 Model

[英]Gaussian Mixture Model with discrete data

我有 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 種均值和方差...任何人都可以幫助我嗎?

您可以使用它,我為您的可視化制作了示例代碼 -

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))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM