Question about a class member

Robert rxjwg98 at gmail.com
Thu Jan 7 12:39:17 EST 2016


On Thursday, January 7, 2016 at 12:24:53 PM UTC-5, Robert wrote:
> Hi,
> 
> I am using a download package. When I read its code, see below please, I 
> don't know what 'sample' is:
> 
> 
> ----------
> model = hmm.GaussianHMM(n_components=4, covariance_type="full")
> 
> model.startprob_ = startprob
> model.transmat_ = transmat
> model.means_ = means
> model.covars_ = covars
> 
> # Generate samples
> X, Z = model.sample(50)
> -------------
> 
> 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)
> 
>     def _init(self, X, lengths=None):
>         super(GMMHMM, self)._init(X, lengths=lengths)
> 
>         for g in self.gmms_:
>             g.set_params(init_params=self.init_params, n_iter=0)
>             g.fit(X)
> 
>     def _compute_log_likelihood(self, X):
>         return np.array([g.score(X) for g in self.gmms_]).T
> 
>     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.
> 
> ..........
> 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' 
> 
> a[1].sample(1,5)
> ---------------------------------------------------------------------------
> AttributeError                            Traceback (most recent call last)
> <ipython-input-71-0364cee47aa3> in <module>()
> ----> 1 a[1].sample(1,5)
> 
> AttributeError: 'float' object has no attribute 'sample'
> ////////////
> 
> What is 'sample' do you think?
> 
> Thanks,

The code can be downloaded from:
https://github.com/hmmlearn/hmmlearn/blob/master/examples/plot_hmm_sampling.py

Hope it can help to answer my question. Thanks again.



More information about the Python-list mailing list