# [SciPy-dev] PyEM problem

David Cournapeau david at ar.media.kyoto-u.ac.jp
Thu Nov 16 05:33:06 CST 2006

```Axel Brink wrote:
> Dear David,
>
> I run into a problem when trying your PyEM example "Creating, sampling
> and plotting a mixture":
>
> Python 2.4.3 (#1, Jun 13 2006, 11:46:08)
> [GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
> >>> import numpy as N
> >>> import pylab as P
> >>> from scipy.sandbox.pyem import GM
> using scipy.cluster.vq
> >>>
> >>> #------------------------------
> ... # Hyper parameters:
> ... #   - K:    number of clusters
> ... #   - d:    dimension
> ... k   = 3
> >>> d   = 2
> >>>
> >>> #-------------------------------------------------------
> ... # Values for weights, mean and (diagonal) variances
> ... #   - the weights are an array of rank 1
> ... #   - mean is expected to be rank 2 with one row for one component
> ... #   - variances are also expteced to be rank 2. For diagonal, one row
> ... #   is one diagonal, for full, the first d rows are the first
> variance,
> ... #   etc... In this case, the variance matrix should be k*d rows and d
> ... #   colums
> ... w   = N.array([0.2, 0.45, 0.35])
> >>> mu  = N.array([[4.1, 3], [1, 5], [-2, -3]])
> >>> va  = N.array([[1, 1.5], [3, 4], [2, 3.5]])
> >>>
> >>> #-----------------------------------------
> ... # First method: directly from parameters:
> ... # Both methods are equivalents.
> ... gm      = GM.fromvalues(w, mu, va)
> >>>
> >>> #-------------------------------------
> ... # Second method to build a GM instance:
> ... gm      = GM(k, d, mode = 'diag')
> >>> # The set_params checks that w, mu, and va corresponds to k, d and m
> ... gm.set_params(w, mu, va)
> Traceback (most recent call last):
>  File "<stdin>", line 2, in ?
> AttributeError: GM instance has no attribute 'set_params'
>
>
> I also tried 'set_param', but this doesn't help either:
>
> >>> gm.set_param(w, mu, va)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File
> "/usr/lib/python2.4/site-packages/scipy/sandbox/pyem/gauss_mix.py",
> line 84, in set_param
>    raise GmParamError("Number of given components is %d, expected %d"
> NameError: global name 'shape' is not defined
>
> Numpy version: 1.0.1.dev3436
> Scipy version: 0.5.2.dev2319
> Can you help me out or point me to someone else who can? Thanks in
Thank you for your interest in pyem.

First, may I request that next time, you set a ticket to scipy tracker ?
It is easier to track things, and everybody has access to it.

Now, there are actually 3 problems in the code ! A syntax error, as you
spot; the example is also wrong because I inverted d and k: GM.__init__
requires first the number of dimension, then the number of components...
The error should have been obvious, if there wasn't a third error, that
is an error in the raised exception string !

I will update the code + doc accordingly, everything should be available
in svn in a few minutes,

cheers,

David
```