Question about a class member

Ian Kelly ian.g.kelly at gmail.com
Thu Jan 7 13:38:52 EST 2016


On Thu, Jan 7, 2016 at 10:23 AM, Robert <rxjwg98 at gmail.com> wrote:
> When I read its (class) definition, I find the following part (which may not
> be sure 100% the above origination yet, but it is the only line being
> 'sample').
> //////////////
>         self.gmms_ = []
>         for x in range(self.n_components):
>             if covariance_type is None:
>                 gmm = GMM(n_mix)
>             else:
>                 gmm = GMM(n_mix, covariance_type=covariance_type)
>             self.gmms_.append(gmm)

self.gmms_ is a list. The contents of the list are instances of the
GMM class, which I can't tell you anything about because it isn't
reproduced in your message.

>     def _generate_sample_from_state(self, state, random_state=None):
>         return self.gmms_[state].sample(1, random_state=random_state).flatten()
> ////////////
>
> The above code looks like self.gmms is a list, which has an attribute
> 'sample'. But when I play with a list, there is no 'sample' attribute.

self.gmms_[state] refers to a particular element of the list, which
per the above should be an instance of the GMM class. The sample
method would be part of the GMM class.

> ..........
> a=[1, 32.0, 4]
>
> a
> Out[68]: [1, 32.0, 4]
>
> type(a)
> Out[69]: list
>
> a[0].sample(1,5)
> ---------------------------------------------------------------------------
> AttributeError                            Traceback (most recent call last)
> <ipython-input-70-ce2e8159b438> in <module>()
> ----> 1 a[0].sample(1,5)
>
> AttributeError: 'int' object has no attribute 'sample'

Because you're playing with a list of ints and floats, not GMMs.



More information about the Python-list mailing list