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


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

numpy array product driving me mad

Started by"Mr. Twister" <mr@twister.com>
First post2015-03-20 13:46 +0100
Last post2015-03-20 14:42 +0100
Articles 4 — 2 participants

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


Contents

  numpy array product driving me mad "Mr. Twister" <mr@twister.com> - 2015-03-20 13:46 +0100
    Re: numpy array product driving me mad Manolo Martínez <manolo@austrohungaro.com> - 2015-03-20 14:11 +0100
    Re: numpy array product driving me mad Manolo Martínez <manolo@austrohungaro.com> - 2015-03-20 14:14 +0100
      Re: numpy array product driving me mad "Mr. Twister" <mr@twister.com> - 2015-03-20 14:42 +0100

#87774 — numpy array product driving me mad

From"Mr. Twister" <mr@twister.com>
Date2015-03-20 13:46 +0100
Subjectnumpy array product driving me mad
Message-ID<meh4s3$sd4$1@speranza.aioe.org>
Hi everyone.

Hope you can help me overcome this "noob" issue.

I have two numpy arrays:

>>> P
array([[[ 2,  3],
        [33, 44],
        [22, 11],
        [ 1,  2]]])
>>> R
array([0, 1, 2, 3])

the values of these may of course be different. The important fact is that:

>>> P.shape
(1, 4, 2)
>>> R.shape
(4,)

where the number 4 in the shape of both P and R may be another number as well
(same on both).


What I'd like to get is a new array Q with same shape as P so that the nth pair
of Q is the nth pair of P multiplied by the nth element of R. I.e., in the above
case it should produce:

>>> Q
array([[[ 0,  0],
        [33, 44],
        [44, 22],
        [ 3,  6]]])


Is there a direct, single expression command to get this result? I have tried
all I could imagine, including .T, extend_dims, h/vstack, repeat..., so far with
no success.

Please, any help will be welcomed.

Thanks.

[toc] | [next] | [standalone]


#87776

FromManolo Martínez <manolo@austrohungaro.com>
Date2015-03-20 14:11 +0100
Message-ID<mailman.38.1426857727.10327.python-list@python.org>
In reply to#87774
On 03/20/15 at 01:46pm, Mr. Twister wrote:
 
> I have two numpy arrays:
> 
> >>> P
> array([[[ 2,  3],
>         [33, 44],
>         [22, 11],
>         [ 1,  2]]])
> >>> R
> array([0, 1, 2, 3])
> 
> the values of these may of course be different. The important fact is that:
> 
> >>> P.shape
> (1, 4, 2)
> >>> R.shape
> (4,)
> 
> where the number 4 in the shape of both P and R may be another number as well
> (same on both).
> 
> 
> What I'd like to get is a new array Q with same shape as P so that the nth pair
> of Q is the nth pair of P multiplied by the nth element of R. I.e., in the above
> case it should produce:
> 
> >>> Q
> array([[[ 0,  0],
>         [33, 44],
>         [44, 22],
>         [ 3,  6]]])
> 
> 
> Is there a direct, single expression command to get this result? 

I think that you want 

    P * R[;,None]

Read about broadcasting
(http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) for an
explanation. I'm never sure I understand it myself :)

Manolo
 

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


#87777

FromManolo Martínez <manolo@austrohungaro.com>
Date2015-03-20 14:14 +0100
Message-ID<mailman.39.1426857853.10327.python-list@python.org>
In reply to#87774
On 03/20/15 at 02:11pm, Manolo Martínez wrote:
> On 03/20/15 at 01:46pm, Mr. Twister wrote:
>  
> > I have two numpy arrays:

[...] 

> > Is there a direct, single expression command to get this result? 
> 
> I think that you want 
> 
>     P * R[;,None]

Sorry, I meant 

    P * R[:, None]

Manolo

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


#87779

From"Mr. Twister" <mr@twister.com>
Date2015-03-20 14:42 +0100
Message-ID<meh83m$5p3$1@speranza.aioe.org>
In reply to#87777
>> I think that you want 
>>
>>     P * R[;,None]
> 
> Sorry, I meant 
> 
>     P * R[:, None]
> 
> Manolo

Muchísimas gracias, Manolo. Eres un genio y me has ayudado mucho. Te debo una.

[toc] | [prev] | [standalone]


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


csiph-web