[Numpy-discussion] field names on numpy arrays
Pierre GM
pgmdevlist at gmail.com
Wed Jun 3 19:20:43 EDT 2009
On Jun 3, 2009, at 5:03 PM, Robert Kern wrote:
> On Wed, Jun 3, 2009 at 15:26, <josef.pktd at gmail.com> wrote:
>> 2009/6/3 Stéfan van der Walt <stefan at sun.ac.za>:
>>> Hi Jon
>>>
>>> 2009/6/3 D2Hitman <j.m.girven at warwick.ac.uk>:
>>>> I understand record arrays such as:
>>>> a_array =
>>>> np.array([(0.,1.,2.,3.,4.),(1.,2.,3.,4.,5.)],dtype=[('a','f'),
>>>> ('b','f'),('c','f'),('d','f'),('e','f')])
>>>> do this with field names.
>>>> a_array['a'] = array([ 0., 1.], dtype=float32)
>>>> however i seem to lose simple operations such as multiplication
>>>> (a_array*2)
>>>> or powers (a_array**2).
>> Why does it not preserve "shape", to do e.g. np.mean by axis?
>
> It does preserve the shape. The input and output are both 1D. If you
> need a different shape (e.g. re-interpreting the record as another
> axis), you need to reshape it yourself. numpy can't guess what you
> want.
Or, as all fields have the same dtype:
>>> a_array.view(dtype=('f',len(a_array.dtype)))
array([[ 0., 1., 2., 3., 4.],
[ 1., 2., 3., 4., 5.]], dtype=float32)
Ain't it fun ?
More information about the NumPy-Discussion
mailing list