[Numpy-discussion] field names on numpy arrays

Stéfan van der Walt stefan at sun.ac.za
Wed Jun 3 14:51:09 EDT 2009


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).

As a workaround, you can have two views on your data:

n [39]: x
Out[39]:
array([(0.0, 1.0, 2.0, 3.0, 4.0), (1.0, 2.0, 3.0, 4.0, 5.0)],
      dtype=[('a', '<f4'), ('b', '<f4'), ('c', '<f4'), ('d', '<f4'),
('e', '<f4')])

In [40]: x = x_dict.view(np.float32)

In [41]: x**2
Out[41]: array([  0.,   1.,   4.,   9.,  16.,   1.,   4.,   9.,  16.,
25.], dtype=float32)

Then you can manipulate the same data using two different "interfaces".

Regards
Stéfan



More information about the NumPy-Discussion mailing list