1

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.

Community
  • 1
  • 1
wow_fan
  • 111
  • 1
  • 10

2 Answers2

3

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])
Yuan JI
  • 2,927
  • 2
  • 20
  • 29
0

Your problem is in the way you are feeding the data to the scatter plot. Particularly you have a numpy array inside a tuple and the way you are indexing is incorrect. Try this.

plt.scatter(Xnew[0][:,0], Xnew[0][:,1])

Basically what we have is that the first indexing Xnew[0] will point to the element in the tuple you want (the numpy array) and the second will slice it as you need. [:,1] Here we take all the rows and the second column.

CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56