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


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

Is there any reason to introduce this intermediate variable (sz)?

Started byfl <rxjwg98@gmail.com>
First post2015-11-17 12:51 -0800
Last post2015-11-18 05:02 -0500
Articles 7 — 5 participants

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


Contents

  Is there any reason to introduce this intermediate variable (sz)? fl <rxjwg98@gmail.com> - 2015-11-17 12:51 -0800
    Re: Is there any reason to introduce this intermediate variable (sz)? John Gordon <gordon@panix.com> - 2015-11-17 21:02 +0000
      Re: Is there any reason to introduce this intermediate variable (sz)? fl <rxjwg98@gmail.com> - 2015-11-17 13:27 -0800
        Re: Is there any reason to introduce this intermediate variable (sz)? John Gordon <gordon@panix.com> - 2015-11-17 22:20 +0000
        Re: Is there any reason to introduce this intermediate variable (sz)? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-11-17 22:33 +0000
    Re: Is there any reason to introduce this intermediate variable (sz)? Dave Farrance <df@see.replyto.invalid> - 2015-11-18 08:05 +0000
    Re: Is there any reason to introduce this intermediate variable (sz)? Terry Reedy <tjreedy@udel.edu> - 2015-11-18 05:02 -0500

#98940 — Is there any reason to introduce this intermediate variable (sz)?

Fromfl <rxjwg98@gmail.com>
Date2015-11-17 12:51 -0800
SubjectIs there any reason to introduce this intermediate variable (sz)?
Message-ID<bad9ac66-38aa-4445-a486-6df0e9c7752c@googlegroups.com>
Hi,

I find the following code snippet, which is useful in my project:


n_iter = 50
sz = (n_iter,) # size of array
x = -0.37727 
z = np.random.normal(x,0.1,size=sz) 

Q = 1e-5 # process variance

# allocate space for arrays
xhat=np.zeros(sz)      
P=np.zeros(sz)         


I learn Python now and the above code seems from an experienced author.
The curious thing to me is the variable 'sz'. I have check np.zeros(shape, ..)

shape : int or sequence of ints


The introduced 'sz' is a tuple. If n_iter function is similar to a constant
in C, I don't see the reason for 'sz'. In fact, 'n_iter' is an int, which fits
the below 

np.zeros(shape)

correctly. Could you see something useful with variable 'sz'?

Thanks,

[toc] | [next] | [standalone]


#98941

FromJohn Gordon <gordon@panix.com>
Date2015-11-17 21:02 +0000
Message-ID<n2g4l9$dqh$1@reader1.panix.com>
In reply to#98940
In <bad9ac66-38aa-4445-a486-6df0e9c7752c@googlegroups.com> fl <rxjwg98@gmail.com> writes:

> correctly. Could you see something useful with variable 'sz'?

'sz' is fewer characters than '(n_iter,)', which may make your code easier
to read.

The np.zeros() function explicitly accepts an 'int or sequence of ints',
so you don't specifically need a sequence.  Is the same true for the
'size' keyword argument of np.random.normal()?

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


#98942

Fromfl <rxjwg98@gmail.com>
Date2015-11-17 13:27 -0800
Message-ID<c9b5f60f-b1ec-4857-bb02-2748a28b5460@googlegroups.com>
In reply to#98941
On Tuesday, November 17, 2015 at 4:03:05 PM UTC-5, John Gordon wrote:
> In <bad9ac66-38aa-4445-a486-6df0e9c7752c@googlegroups.com> fl <@gmail.com> writes:
> 
> > correctly. Could you see something useful with variable 'sz'?
> 
> 'sz' is fewer characters than '(n_iter,)', which may make your code easier
> to read.
> 
> The np.zeros() function explicitly accepts an 'int or sequence of ints',
> so you don't specifically need a sequence.  Is the same true for the
> 'size' keyword argument of np.random.normal()?
> 
> -- 
> John Gordon                   A is for Amy, who fell down the stairs
> @panix.com              B is for Basil, assaulted by bears
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"

Hi, I get the following for the third parameter of np.random.normal():
size : int or tuple of ints, optional

I still don't see the necessity of 'sz'. Thanks,

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


#98943

FromJohn Gordon <gordon@panix.com>
Date2015-11-17 22:20 +0000
Message-ID<n2g97m$qf6$1@reader1.panix.com>
In reply to#98942
In <c9b5f60f-b1ec-4857-bb02-2748a28b5460@googlegroups.com> fl <rxjwg98@gmail.com> writes:

> I still don't see the necessity of 'sz'. Thanks,

sz isn't required.  You can use (n_iter,) in place of sz.

However, as I posted earlier, sz is shorter so it might make your code
easier to read.

Using sz can also lead to easier code maintenance.  If the contents of
the tuple were ever to change, it would be much easier to simply change
it in once place (the definition of sz), rather than having to edit
several different occurrences of (n_iter,) in your code.


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


#98944

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-11-17 22:33 +0000
Message-ID<mailman.401.1447799666.16136.python-list@python.org>
In reply to#98942
On 17/11/2015 21:27, fl wrote:
> On Tuesday, November 17, 2015 at 4:03:05 PM UTC-5, John Gordon wrote:
>> In <bad9ac66-38aa-4445-a486-6df0e9c7752c@googlegroups.com> fl <@gmail.com> writes:
>>
>>> correctly. Could you see something useful with variable 'sz'?
>>
>> 'sz' is fewer characters than '(n_iter,)', which may make your code easier
>> to read.
>>
>> The np.zeros() function explicitly accepts an 'int or sequence of ints',
>> so you don't specifically need a sequence.  Is the same true for the
>> 'size' keyword argument of np.random.normal()?
>>
>> --
>> John Gordon                   A is for Amy, who fell down the stairs
>> @panix.com              B is for Basil, assaulted by bears
>>                                  -- Edward Gorey, "The Gashlycrumb Tinies"
>
> Hi, I get the following for the third parameter of np.random.normal():
> size : int or tuple of ints, optional
>
> I still don't see the necessity of 'sz'. Thanks,
>

I don't see the necessity for you to bombard this list with question 
after question without bothering to try and provide your own answers 
first.  Did your last skivvy die of overwork?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


#98949

FromDave Farrance <df@see.replyto.invalid>
Date2015-11-18 08:05 +0000
Message-ID<auao4btnismb7i1r1fi8tpi8f2s1asvmig@4ax.com>
In reply to#98940
fl <rxjwg98@gmail.com> wrote:

>Hi,
>I find the following code snippet, which is useful in my project:
> ...
>correctly. Could you see something useful with variable 'sz'?

So that's example code in "An Introduction to the Kalman Filter" by Greg
Welch and Gary Bishop, and no, that construct was unnecessary. As you've
figured out, you can use the integer directly.

It's the usual problem that people get when they switch from using
Octave/Matlab to numpy/scipy/matplotlib. The former are better in an
interactive maths-oriented environment, but people then often then
switch to Python for more complex algorithms to take advantage of the
features of an advanced general-purpose programming language. The
problem that people then run into is that although there are equivalents
in the Python libraries for most of the Matlab functions, this happens
to be an area where the documentation is particularly uneven.

"Pylab" is a project that attempted to be a Python equivalent of Matlab,
but has now become a depreciated appendix to matplotlib. There have been
attempts to restart it as a feature of Ipython, but although it mostly
works, the documentation is almost nonexistent. The only way to figure
out what it can do is to try it yourself with a lot of trial and error.

Anyway, don't be surprised if you see unnecessary elaborations in
maths/science Python code because it's what you expect when people are
arriving at code that works from reading poor documentation, trial and
error, and Googling other peoples code snippets. Just try it yourself
and save yourself time rather than asking for hand-holding.

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


#98955

FromTerry Reedy <tjreedy@udel.edu>
Date2015-11-18 05:02 -0500
Message-ID<mailman.408.1447840942.16136.python-list@python.org>
In reply to#98940
On 11/17/2015 3:51 PM, fl wrote:

> n_iter = 50
> sz = (n_iter,) # size of array
> x = -0.37727
> z = np.random.normal(x,0.1,size=sz)
>
> Q = 1e-5 # process variance
>
> # allocate space for arrays
> xhat=np.zeros(sz)
> P=np.zeros(sz)
>
>
> I learn Python now and the above code seems from an experienced author.
> The curious thing to me is the variable 'sz'.

'sz' is a name and in the program above, it is a constant tuple derived 
from constant int n_iter.  Since the tuple is used more than once, 
calculating it just once and naming it is a good idea.

 > I have check np.zeros(shape, ..)

Reading about the the signature of functions that you use or read about 
is a good idea and quite normal.

> shape : int or sequence of ints

> The introduced 'sz' is a tuple.

and tuples are sequences.

 > If n_iter function is similar to a constant in C,

n_iter is a named constant, not a function, as you note below.

 > I don't see the reason for 'sz'.

I gave a reason for it existig above, but I think you are asking about 
its use in the particular location.

> In fact, 'n_iter' is an int, which fits  the below
>
> np.zeros(shape)
>
> correctly.  Could you see something useful with variable 'sz'?

I would presume until I tried it that np.zeros(50) and np.zeros((50,)) 
are different (have a different shape).  If they are not, then the use 
of a singleton tuple is confusing, whether or not the tuple is given a name.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


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


csiph-web