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


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

What use of 'sum' in this line code?

Started byRobert <rxjwg98@gmail.com>
First post2016-01-03 16:28 -0800
Last post2016-01-03 18:44 -0800
Articles 5 — 4 participants

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


Contents

  What use of 'sum' in this line code? Robert <rxjwg98@gmail.com> - 2016-01-03 16:28 -0800
    Re: What use of 'sum' in this line code? Steven D'Aprano <steve@pearwood.info> - 2016-01-04 11:43 +1100
    Re: What use of 'sum' in this line code? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-01-04 00:48 +0000
    Re: What use of 'sum' in this line code? Peter Otten <__peter__@web.de> - 2016-01-04 01:53 +0100
      Re: What use of 'sum' in this line code? Robert <rxjwg98@gmail.com> - 2016-01-03 18:44 -0800

#101219 — What use of 'sum' in this line code?

FromRobert <rxjwg98@gmail.com>
Date2016-01-03 16:28 -0800
SubjectWhat use of 'sum' in this line code?
Message-ID<b3f36467-331d-4262-868f-22583c33ba72@googlegroups.com>
Hi,

I find below code snippet on line:


//////////
m = 10
theta_A = 0.8
theta_B = 0.3
theta_0 = [theta_A, theta_B]

coin_A = bernoulli(theta_A)
coin_B = bernoulli(theta_B)

xs = map(sum, [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m), coin_B.rvs(m)])
/////////

I see 
[coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m), coin_B.rvs(m)]

is simply a list, but I don't know what use of 'sum' in this line.
I replace the random number with a simple list:
///////
yy=map(sum, [13, 22, 33, 41])

In [24]: yy
Out[24]: [13, 22, 33, 41]
///////

I don't see 'sum' has any effect above.
The code source is from:
#http://people.duke.edu/~ccc14/sta-663/EMAlgorithm.html


What could you help me on the 'sum'?

Thanks,

[toc] | [next] | [standalone]


#101220

FromSteven D'Aprano <steve@pearwood.info>
Date2016-01-04 11:43 +1100
Message-ID<5689c03f$0$1621$c3e8da3$5496439d@news.astraweb.com>
In reply to#101219
On Mon, 4 Jan 2016 11:28 am, Robert wrote:

> Hi,
> 
> I find below code snippet on line:
> 
> 
> //////////
> m = 10
> theta_A = 0.8
> theta_B = 0.3
> theta_0 = [theta_A, theta_B]
> 
> coin_A = bernoulli(theta_A)
> coin_B = bernoulli(theta_B)
> 
> xs = map(sum, [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m),
> coin_B.rvs(m)]) /////////
> 
> I see
> [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m),
> [coin_B.rvs(m)]
> 
> is simply a list, 


A list of what? Without knowing what coin_A.rvs(m) returns, it is impossible
to know what sum will do.



> but I don't know what use of 'sum' in this line. 
> I replace the random number with a simple list:
> ///////
> yy=map(sum, [13, 22, 33, 41])

> In [24]: yy 
> Out[24]: [13, 22, 33, 41]

I do not get that result. I get an error:


py> yy = map(sum, [13, 22, 33, 41])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable




Try replacing the list-of-mystery-things with a list of lists:


map(sum, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])

and see what you get.



-- 
Steven

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


#101221

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-01-04 00:48 +0000
Message-ID<87ziwmrwyq.fsf@bsb.me.uk>
In reply to#101219
Robert <rxjwg98@gmail.com> writes:

> I find below code snippet on line:
>
> //////////
> m = 10
> theta_A = 0.8
> theta_B = 0.3
> theta_0 = [theta_A, theta_B]
>
> coin_A = bernoulli(theta_A)
> coin_B = bernoulli(theta_B)
>
> xs = map(sum, [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m),
> coin_A.rvs(m), coin_B.rvs(m)])
> /////////
>
> I see 
> [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m), coin_B.rvs(m)]
>
> is simply a list, but I don't know what use of 'sum' in this line.
> I replace the random number with a simple list:
> ///////
> yy=map(sum, [13, 22, 33, 41])
>
> In [24]: yy
> Out[24]: [13, 22, 33, 41]
> ///////
>
> I don't see 'sum' has any effect above.

map applies the first argument (sum) to the elements of the second.  You
won't see any effect unless these elements are sequences that can be
summed.  For example:

  map(sum, [[1, 2], [2, 3], [3, 4]])

bernoulli(theta_A) is a statistical distribution, with the parameter
frozen in ready to have samples drawn from it.  coin_A.rvs(10) requests
10 random variates from the distribution -- it's an array containing ten
0/1 elements.  Thus the map(sum, ...) call does have an array or array
to work with.

[Not being a Python expect I've probably got some of the terminology
wrong but I hope the gist of it clear.]

<snip>
-- 
Ben.

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


#101223

FromPeter Otten <__peter__@web.de>
Date2016-01-04 01:53 +0100
Message-ID<mailman.1.1451868841.2305.python-list@python.org>
In reply to#101219
Robert wrote:

> Hi,
> 
> I find below code snippet on line:
> 
> 
> //////////
> m = 10
> theta_A = 0.8
> theta_B = 0.3
> theta_0 = [theta_A, theta_B]
> 
> coin_A = bernoulli(theta_A)
> coin_B = bernoulli(theta_B)
> 
> xs = map(sum, [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m),
> coin_B.rvs(m)]) /////////
> 
> I see
> [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m),
> [coin_B.rvs(m)]
> 
> is simply a list, but I don't know what use of 'sum' in this line.
> I replace the random number with a simple list:
> ///////
> yy=map(sum, [13, 22, 33, 41])
> 
> In [24]: yy
> Out[24]: [13, 22, 33, 41]
> ///////
> 
> I don't see 'sum' has any effect above.
> The code source is from:
> #http://people.duke.edu/~ccc14/sta-663/EMAlgorithm.html
> 
> 
> What could you help me on the 'sum'?

>>> import numpy
>>> values = [13, 22, 33, 41]
>>> map(numpy.sum, values)
[13, 22, 33, 41]
>>> values2 = [[1, 2], [3, 4]]
>>> map(numpy.sum, values2)
[3, 7]

In Python 2 map(sum, values) applies sum to every value in the list and 
returns the resulting list of sums. Apparently the numpy developers found it 
convenient that sum(scalar) == scalar holds.


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


#101224

FromRobert <rxjwg98@gmail.com>
Date2016-01-03 18:44 -0800
Message-ID<23b6741e-55cf-4596-8fad-43c90c1e8556@googlegroups.com>
In reply to#101223
On Sunday, January 3, 2016 at 7:54:13 PM UTC-5, Peter Otten wrote:
> Robert wrote:
> 
> > Hi,
> > 
> > I find below code snippet on line:
> > 
> > 
> > //////////
> > m = 10
> > theta_A = 0.8
> > theta_B = 0.3
> > theta_0 = [theta_A, theta_B]
> > 
> > coin_A = bernoulli(theta_A)
> > coin_B = bernoulli(theta_B)
> > 
> > xs = map(sum, [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m),
> > coin_B.rvs(m)]) /////////
> > 
> > I see
> > [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m),
> > [coin_B.rvs(m)]
> > 
> > is simply a list, but I don't know what use of 'sum' in this line.
> > I replace the random number with a simple list:
> > ///////
> > yy=map(sum, [13, 22, 33, 41])
> > 
> > In [24]: yy
> > Out[24]: [13, 22, 33, 41]
> > ///////
> > 
> > I don't see 'sum' has any effect above.
> > The code source is from:
> > #http://people.duke.edu/~ccc14/sta-663/EMAlgorithm.html
> > 
> > 
> > What could you help me on the 'sum'?
> 
> >>> import numpy
> >>> values = [13, 22, 33, 41]
> >>> map(numpy.sum, values)
> [13, 22, 33, 41]
> >>> values2 = [[1, 2], [3, 4]]
> >>> map(numpy.sum, values2)
> [3, 7]
> 
> In Python 2 map(sum, values) applies sum to every value in the list and 
> returns the resulting list of sums. Apparently the numpy developers found it 
> convenient that sum(scalar) == scalar holds.

Thanks, all you say are correct in one way or the other. I just notice that
it uses coin_A.rvs(m) (m=10). Thus, it sums 10 random numbers.

[toc] | [prev] | [standalone]


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


csiph-web