Rachael
Rachael

Reputation: 315

How can I sample the different components of a GMM distribution?

I have clustered my data (12000, 3) using sklearn Gaussian mixture model algorithm (GMM). I have 3 clusters. Each point of my data represents a molecular structure. I would like to know how could I sampled each cluster. I have tried with the function:

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

but it does preform a sampling of the whole distribution, but I need a sample of each one of the components.

Upvotes: 0

Views: 1789

Answers (1)

pythonic833
pythonic833

Reputation: 3224

Well this is not that easy since you need to calculate the eigenvectors of all covariance matrices. Here is some example code for a problem I studied

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

So for my problem the resulting plot looked like this:enter image description here

Edit: here the answer to your comment. I would use pandas to do this. Assume X is your feature matrix and y are your labels, then

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

In the resulting dataframe you can check all the information you want, you can even just exclude the samples the algorithm misclassified with:

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

Upvotes: 1

Related Questions