[Numpy-discussion] array with named columns (or record arrays with homogenous types)

Tony Yu tsyu80@gmail....
Mon Sep 22 23:06:14 CDT 2008


>
> Message: 8
> Date: Mon, 22 Sep 2008 19:28:50 -0400
> From: Pierre GM <pgmdevlist@gmail.com>
> Subject: Re: [Numpy-discussion] array with named columns (or record
> 	arrays	with homogenous types)
> To: Discussion of Numerical Python <numpy-discussion@scipy.org>
> Message-ID: <200809221928.50535.pgmdevlist@gmail.com>
> Content-Type: text/plain;  charset="iso-8859-1"
>
> On Monday 22 September 2008 19:25:16 Tony S Yu wrote:
>
>>>>> import numpy as np
>>>>>
>>>>> color = np.array([(1, 2, 3), (4, 5, 6)],
>>
>> ...                  dtype=[('r', '<f4'), ('g', '<f4'), ('b',  
>> '<f4')])
>> # get the average (grayscale) of the first row
>>
>>>>> color[0].mean()
>
> What about
> color.view((float,3))[0].mean() ?


Yeah, that works perfectly; by specifying the axis and dropping the  
index (`color.view(('<f4',3)).mean(axis=1)) gives the mean of all the  
records.

BTW, is the tuple argument you use with `view` documented anywhere; I  
haven't seen it before and a quick search doesn't give any results.

Thanks!
-Tony


More information about the Numpy-discussion mailing list