【问题标题】:How can I sample the different components of a GMM distribution?如何对 GMM 分布的不同分量进行采样?
【发布时间】:2018-09-07 14:32:20
【问题描述】:

我使用 sklearn 高斯混合模型算法 (GMM) 对我的数据 (12000, 3) 进行了聚类。我有 3 个集群。我的数据的每个点都代表一个分子结构。我想知道如何对每个集群进行采样。我已经尝试过该功能:

gmm = GMM(n_components=3).fit(Data)
gmm.sample(n_samples=20)

但它确实对整个分布进行了抽样,但我需要对每个组件进行抽样。

【问题讨论】:

    标签: python scikit-learn gmm


    【解决方案1】:

    这并不容易,因为您需要计算所有协方差矩阵的特征向量。这是我研究的一个问题的一些示例代码

    import numpy as np
    from scipy.stats import multivariate_normal
    import random
    from operator import truediv
    import itertools
    from scipy import linalg
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    from sklearn import mixture
    
    #import some data which can be used for gmm
    mix = np.loadtxt("mixture.txt", usecols=(0,1), unpack=True)
    #print(mix.shape)
    color_iter = itertools.cycle(['navy', 'c', 'cornflowerblue', 'gold',
                                  'darkorange'])
    
    def plot_results(X, Y_, means, covariances, index, title):
    #function for plotting the gaussians
        splot = plt.subplot(2, 1, 1 + index)
        for i, (mean, covar, color) in enumerate(zip(
                means, covariances, color_iter)):
            v, w = linalg.eigh(covar)
            v = 2. * np.sqrt(2.) * np.sqrt(v)
            u = w[0] / linalg.norm(w[0])
            # as the DP will not use every component it has access to
            # unless it needs it, we shouldn't plot the redundant
            # components.
            if not np.any(Y_ == i):
                continue
            plt.scatter(X[Y_ == i, 0], X[Y_ == i, 1], .8, color=color)
    
            # Plot an ellipse to show the Gaussian component
            angle = np.arctan(u[1] / u[0])
            angle = 180. * angle / np.pi  # convert to degrees
            ell = mpl.patches.Ellipse(mean, v[0], v[1], 180. + angle, color=color)
            ell.set_clip_box(splot.bbox)
            ell.set_alpha(0.5)
            splot.add_artist(ell)
    
        plt.xlim(-4., 3.)
        plt.ylim(-4., 2.)
    
    gmm = mixture.GaussianMixture(n_components=3, covariance_type='full').fit(mix.T)
    print(gmm.predict(mix.T))
    plot_results(mix.T, gmm.predict(mix.T), gmm.means_, gmm.covariances_, 0,
                 'Gaussian Mixture')   
    

    所以对于我的问题,结果图如下所示:

    编辑:这里是您评论的答案。我会使用熊猫来做到这一点。假设X 是你的特征矩阵,y 是你的标签,那么

    import pandas as pd
    y_pred = gmm.predict(X)
    df_all_info = pd.concat([X,y,y_pred], axis=1)
    

    在生成的数据框中,您可以检查所需的所有信息,甚至可以排除算法错误分类的样本:

    df_wrong = df_all_info[df_all_info['name of y-column'] != df_all_info['name of y_pred column']]
    

    【讨论】:

    • 这不是我的意思。我确实有一个像你这样的图表。我的数据已经分布在三个集群中,我可以以图形方式查看集群,在我的例子中,我使用了球体,因为它是一个 3D 图。我想知道数据中的哪个点对应于每个集群。例如,如果每一行都是一个分子结构,我想知道第 7 行所代表的结构是属于簇 1、2 还是 3,等等。那,或者我可以链接到 X 结构的每个集群的样本。我不确定我是否解释正确
    • 检查我的编辑,这应该是你最初想要的答案
    • 这真的很有帮助!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多