pythonscikit-learndata-sciencegaussianmixture-model

Sampling data points from a Gaussian Mixture Model python


I am really new to python and GMM. I learned GMM recently and trying to implement the codes from here

I met some problems when I run gmm.sample() method:

gmm16 = GaussianMixture(n_components=16, covariance_type='full', random_state=0)    
Xnew = gmm16.sample(400,random_state=42)
plt.scatter(Xnew[:, 0], Xnew[:, 1])

the error shows:

TypeError: sample() got an unexpected keyword argument 'random_state'

I have checked the latest document and find out the method sample should only contain n which indicates that the number of samples to generate. But when I delete 'random_state=42', new error appears:

codes:

Xnew = gmm16.sample(400)
plt.scatter(Xnew[:, 0], Xnew[:, 1])

error:

TypeError: tuple indices must be integers or slices, not tuple

Does anyone meet this problem when you implement the codes from Jake VanderPlas? How could I fix it?

My Jupyter:

The version of the notebook server is: 5.7.4

Python 3.7.1 (default, Dec 14 2018, 13:28:58)

Type 'copyright', 'credits' or 'license' for more information

IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.


Solution

  • You get the TypeError because the sample method returns a tuple, see here.

    This should do the job:

    Xnew, Ynew = gmm16.sample(400)  # if Ynew is valuable
    plt.scatter(Xnew[:, 0], Xnew[:, 1])
    

    or

    Xnew, _ = gmm16.sample(400)  # if Ynew isn't valuable
    plt.scatter(Xnew[:, 0], Xnew[:, 1])