【问题标题】:Gaussian Mixture Model with discrete data具有离散数据的高斯混合模型
【发布时间】:2020-11-04 09:57:05
【问题描述】:

我有 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), ...

因此,通过上述过程,对于xy平面上以第一个给定数据为y值的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 种均值和方差...任何人都可以帮助我吗?

【问题讨论】:

    标签: python numpy gaussian


    【解决方案1】:

    你可以用这个,我已经为你的可视化做了一个示例代码 -

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

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 2021-09-30
      • 2012-07-08
      • 1970-01-01
      • 2023-03-24
      • 2014-01-09
      • 2012-05-08
      • 2018-07-19
      • 2013-06-11
      相关资源
      最近更新 更多