【问题标题】:multi dimensional fit with gaussian mixture model高斯混合模型的多维拟合
【发布时间】:2018-07-19 04:15:03
【问题描述】:

我发现可以使用 sklearn.mixture.GaussianMixture 将高斯混合模型拟合到 sklearn 的一维信号(例如直方图,参见第一张图片)(参见 here

我现在想在二维中拟合高斯混合模型(例如,参见第二张图片)。这也可能吗?

【问题讨论】:

    标签: machine-learning scikit-learn gmm


    【解决方案1】:

    您可以应用相同的方法。首先使模型适合数据(现在它有 2 列代表 x 和 y 值)。然后计算你想要的任何点的可能性。

    在下面的示例中,点是从 2 个高斯分布生成的。

    # Generate Data
    data1 = np.random.multivariate_normal( [3,3], [[1, 0], [0, 1]], 80)
    data2 = np.random.multivariate_normal( [-4,2], [[1, 1.5], [1.5, 3]], 120)
    
    X = np.concatenate((data1, data2))
    plt.axis([-8, 8, -8, 8])
    plt.scatter(X.T[0], X.T[1])
    plt.show()
    

    然后拟合数据。请注意,您应该知道分配的数量(在本例中为 2)。

    # Fit the data
    gmm = GaussianMixture(n_components=2)
    gmm.fit(X)
    

    现在,您已经估计了分布参数。您可以使用它们计算任何点的概率。

    # Distribution parameters
    print(gmm.means_)
    [[-3.87809034  2.15419139]
     [ 3.07939341  3.02521961]]
    
    print(gmm.covariances_)
    [[[ 0.78359622  1.11780271]
      [ 1.11780271  2.31658265]]
    
     [[ 0.80263971 -0.03346032]
      [-0.03346032  1.04663585]]]
    

    score_sample 方法可以代替。此方法返回给定点的对数似然度。

    lin_param = (-8, 8, 100)
    x = np.linspace(*lin_param)
    y = np.linspace(*lin_param)
    
    xx, yy = np.meshgrid(x, y)
    pos = np.concatenate((xx.reshape(-1, 1), yy.reshape(-1, 1)), axis = 1)
    z = gmm.score_samples(pos) # Note that this method returns log-likehood
    # z = np.exp(gmm.score_samples(pos)) # e^x to get likehood values
    z = z.reshape(xx.shape)
    
    plt.contourf(x, y, z, 50, cmap="viridis")
    plt.show()
    

    【讨论】:

    • 我不想让 gmm 适合散点,而是适合二维数组。所以我想知道最好地重建二维数组 arr 的每个像素的均值/协方差,形状为 arr.shape = (height, width)
    猜你喜欢
    • 2017-02-26
    • 2014-08-02
    • 2014-10-08
    • 2021-09-30
    • 2018-04-08
    • 2023-03-24
    • 2014-01-09
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多