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


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

numpy column_stack - why does this work?

Started byPythonDude <mjoerg.phone@gmail.com>
First post2015-11-13 07:37 -0800
Last post2015-11-16 00:02 -0800
Articles 3 — 2 participants

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


Contents

  numpy column_stack - why does this work? PythonDude <mjoerg.phone@gmail.com> - 2015-11-13 07:37 -0800
    Re: numpy column_stack - why does this work? Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-13 10:16 -0700
      Re: numpy column_stack - why does this work? PythonDude <mjoerg.phone@gmail.com> - 2015-11-16 00:02 -0800

#98745 — numpy column_stack - why does this work?

FromPythonDude <mjoerg.phone@gmail.com>
Date2015-11-13 07:37 -0800
Subjectnumpy column_stack - why does this work?
Message-ID<0b4725a8-a040-4bfb-b046-81c2e529447a@googlegroups.com>
Hi all,

Just a quick question about this code-piece (it works, I've tested it):

means, stds = np.column_stack([
    getMuSigma_from_PF(return_vec) 
    for _ in xrange(n_portfolios) ])


1) I understand column_stack does this (assembles vectors vertically, side-by-side):

>>> a = np.array((1,2,3)) # NB: a is row-vector: {1 2 3}
>>> b = np.array((2,3,4)) # NB: b is also a row-vector...
>>> np.column_stack((a,b))
array([[1, 2],
       [2, 3],
       [3, 4]])

2) I understand the underscore is just a "dummy variable" in the last line "for _ in xrange(n_portfolios)" - this also looked a bit confusing to me, at first...

3) I DON'T understand why the code doesn't look like this:

means, stds = np.column_stack([
    for _ in xrange(n_portfolios):
      getMuSigma_from_PF(return_vec) ])

???

Any comments/advice/hints, I would appreciate from you, thank you!

[toc] | [next] | [standalone]


#98751

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-11-13 10:16 -0700
Message-ID<mailman.293.1447435057.16136.python-list@python.org>
In reply to#98745
On Fri, Nov 13, 2015 at 8:37 AM, PythonDude <mjoerg.phone@gmail.com> wrote:
> Hi all,
>
> Just a quick question about this code-piece (it works, I've tested it):
>
> means, stds = np.column_stack([
>     getMuSigma_from_PF(return_vec)
>     for _ in xrange(n_portfolios) ])
>
>
> 1) I understand column_stack does this (assembles vectors vertically, side-by-side):
>
>>>> a = np.array((1,2,3)) # NB: a is row-vector: {1 2 3}
>>>> b = np.array((2,3,4)) # NB: b is also a row-vector...
>>>> np.column_stack((a,b))
> array([[1, 2],
>        [2, 3],
>        [3, 4]])
>
> 2) I understand the underscore is just a "dummy variable" in the last line "for _ in xrange(n_portfolios)" - this also looked a bit confusing to me, at first...
>
> 3) I DON'T understand why the code doesn't look like this:
>
> means, stds = np.column_stack([
>     for _ in xrange(n_portfolios):
>       getMuSigma_from_PF(return_vec) ])

Because that would be invalid syntax; you can't put a for loop inside
an expression like that. Your question is not about numpy.column_stack
at all, but about list comprehensions. I suggest you start by reading
this:

https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

Then if you're still confused, come back and ask further questions.

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


#98869

FromPythonDude <mjoerg.phone@gmail.com>
Date2015-11-16 00:02 -0800
Message-ID<96d95669-5cf5-4a33-ba51-878edd975e48@googlegroups.com>
In reply to#98751
On Friday, 13 November 2015 18:17:59 UTC+1, Ian  wrote:
> On Fri, Nov 13, 2015 at 8:37 AM, PythonDude <mjoexxxxx.com> wrote:
> > 3) I DON'T understand why the code doesn't look like this:
> >
> > means, stds = np.column_stack([
> >     for _ in xrange(n_portfolios):
> >       getMuSigma_from_PF(return_vec) ])
> 
> Because that would be invalid syntax; you can't put a for loop inside
> an expression like that. Your question is not about numpy.column_stack
> at all, but about list comprehensions. I suggest you start by reading
> this:
> 
> https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
> 
> Then if you're still confused, come back and ask further questions.

Thank you very much, I'll look careful into that before asking again :-)

[toc] | [prev] | [standalone]


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


csiph-web