Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101346 > unrolled thread

Question about a class member

Started byRobert <rxjwg98@gmail.com>
First post2016-01-07 09:23 -0800
Last post2016-01-07 15:02 -0800
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Question about a class member Robert <rxjwg98@gmail.com> - 2016-01-07 09:23 -0800
    Re: Question about a class member Robert <rxjwg98@gmail.com> - 2016-01-07 09:39 -0800
    Re: Question about a class member John Gordon <gordon@panix.com> - 2016-01-07 18:37 +0000
    Re: Question about a class member Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-07 11:38 -0700
    Re: Question about a class member Steven D'Aprano <steve@pearwood.info> - 2016-01-08 09:05 +1100
      Re: Question about a class member Robert <rxjwg98@gmail.com> - 2016-01-07 15:02 -0800

#101346 — Question about a class member

FromRobert <rxjwg98@gmail.com>
Date2016-01-07 09:23 -0800
SubjectQuestion about a class member
Message-ID<a199a9b7-784e-4e12-8030-4e3fffd636a7@googlegroups.com>
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,

[toc] | [next] | [standalone]


#101347

FromRobert <rxjwg98@gmail.com>
Date2016-01-07 09:39 -0800
Message-ID<106d3b9b-a9ba-4c9e-abe8-ae06381b6bd2@googlegroups.com>
In reply to#101346
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.

[toc] | [prev] | [next] | [standalone]


#101348

FromJohn Gordon <gordon@panix.com>
Date2016-01-07 18:37 +0000
Message-ID<n6mb9p$93n$1@reader1.panix.com>
In reply to#101346
In <a199a9b7-784e-4e12-8030-4e3fffd636a7@googlegroups.com> Robert <rxjwg98@gmail.com> writes:

> 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)
> -------------

sample() is a method in the GaussianHMM class.  (In this case, it's
a method in the _BaseHMM class, from which GaussianHMM inherits.)

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#101349

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-01-07 11:38 -0700
Message-ID<mailman.52.1452191974.2305.python-list@python.org>
In reply to#101346
On Thu, Jan 7, 2016 at 10:23 AM, Robert <rxjwg98@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.

[toc] | [prev] | [next] | [standalone]


#101351

FromSteven D'Aprano <steve@pearwood.info>
Date2016-01-08 09:05 +1100
Message-ID<568ee11c$0$1596$c3e8da3$5496439d@news.astraweb.com>
In reply to#101346
On Fri, 8 Jan 2016 04:23 am, 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")


When I try running that code, I get an error:


py> model = hmm.GaussianHMM(n_components=4, covariance_type="full")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'hmm' is not defined

What's hmm? Where does it come from? Is it this?

https://hmmlearn.github.io/hmmlearn/generated/hmmlearn.hmm.GaussianHMM.html

It has a sample method here:

https://hmmlearn.github.io/hmmlearn/generated/hmmlearn.hmm.GaussianHMM.html#hmmlearn.hmm.GaussianHMM.sample


You should try googling for help before asking questions:

https://duckduckgo.com/html/?q=hmm.GaussianHMM

or use the search engine of your choice.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#101352

FromRobert <rxjwg98@gmail.com>
Date2016-01-07 15:02 -0800
Message-ID<e7259fd7-2671-49ba-a8f0-e802f6dba127@googlegroups.com>
In reply to#101351
On Thursday, January 7, 2016 at 5:06:07 PM UTC-5, Steven D'Aprano wrote:
> On Fri, 8 Jan 2016 04:23 am, 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")
> 
> 
> When I try running that code, I get an error:
> 
> 
> py> model = hmm.GaussianHMM(n_components=4, covariance_type="full")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'hmm' is not defined
> 
> What's hmm? Where does it come from? Is it this?
> 
> https://hmmlearn.github.io/hmmlearn/generated/hmmlearn.hmm.GaussianHMM.html
> 
> It has a sample method here:
> 
> https://hmmlearn.github.io/hmmlearn/generated/hmmlearn.hmm.GaussianHMM.html#hmmlearn.hmm.GaussianHMM.sample
> 
> 
> You should try googling for help before asking questions:
> 
> https://duckduckgo.com/html/?q=hmm.GaussianHMM
> 
> or use the search engine of your choice.
> 
> 
> -- 
> Steven

Thanks. I just realized that my list assumption was wrong. I got that 
conclusion was incorrect.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web