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


Groups > comp.lang.python > #98751

Re: numpy column_stack - why does this work?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: numpy column_stack - why does this work?
Date Fri, 13 Nov 2015 10:16:49 -0700
Lines 35
Message-ID <mailman.293.1447435057.16136.python-list@python.org> (permalink)
References <0b4725a8-a040-4bfb-b046-81c2e529447a@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de /kNHuSslYySsD28FBo8qoAWubOjqowR2HKEtn5YgHPUw==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.021
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'means,': 0.07; 'nb:': 0.09; 'subject:why': 0.09; 'underscore': 0.09; 'suggest': 0.15; '2],': 0.16; '3],': 0.16; '[2,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'stds': 0.16; 'vectors': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'all,': 0.20; '2015': 0.20; 'am,': 0.23; 'bit': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; "i've": 0.25; "doesn't": 0.26; 'fri,': 0.27; 'question': 0.27; 'message- id:@mail.gmail.com': 0.27; '(it': 0.29; '13,': 0.29; 'that.': 0.30; 'code': 0.30; 'skip:g 30': 0.30; "can't": 0.32; 'url:python': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'nov': 0.35; 'but': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'subject:work': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'why': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'url:3': 0.60; 'your': 0.60; 'further': 0.62; 'back': 0.62; 'skip:n 10': 0.62; 'confusing': 0.84; 'syntax;': 0.84; 'to:name:python': 0.84; 'subject:this': 0.85; 'url:tutorial': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=RK1x9Ud1bOtBMdw410C2BuYDkK9OnkbltiPWz6fA7mw=; b=fPvHDIzXcTC9ysuEe3/ZuPsdCIWmSNBYoia7X/WUO4jqJZ83h7S36TQO3J8tAhUuNp 2MYYWts1GZ1ZkYYmFRp6F0/kPhDjK7VvmeMFo2SyFflWbqR/DT/3FNOyfSPyHjJzohV3 fOnaYoukxV3EkJWRKNZtVVxdyeF/07YAyOP4cBnaeoZJSmLYld7vSQ0jAkgU3FrYQQ8/ /Io1witI1PzVfUT0zDgl30vF01yHpPBVQCXNrTTGKKiLttPnQZEutWRb0Y1DWblgnQtY X9ysvFxI6cCTKbGFNYbnhVheb99xPwA8UUGcavsdg76VjCdIquXwDEp4t2YpsMh1FFtv 3Vlg==
X-Received by 10.50.43.129 with SMTP id w1mr4557283igl.93.1447435049240; Fri, 13 Nov 2015 09:17:29 -0800 (PST)
In-Reply-To <0b4725a8-a040-4bfb-b046-81c2e529447a@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98751

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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