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


Groups > comp.lang.python > #98745

numpy column_stack - why does this work?

Newsgroups comp.lang.python
Date 2015-11-13 07:37 -0800
Message-ID <0b4725a8-a040-4bfb-b046-81c2e529447a@googlegroups.com> (permalink)
Subject numpy column_stack - why does this work?
From PythonDude <mjoerg.phone@gmail.com>

Show all headers | View raw


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!

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web