[Numpy-discussion] field names on numpy arrays

Robert Kern robert.kern@gmail....
Wed Jun 3 18:23:06 CDT 2009


On Wed, Jun 3, 2009 at 18:20, Pierre GM <pgmdevlist@gmail.com> wrote:
>
> On Jun 3, 2009, at 5:03 PM, Robert Kern wrote:
>
>> On Wed, Jun 3, 2009 at 15:26,  <josef.pktd@gmail.com> wrote:
>>> 2009/6/3 Stéfan van der Walt <stefan@sun.ac.za>:
>>>> Hi Jon
>>>>
>>>> 2009/6/3 D2Hitman <j.m.girven@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 ?

Ah, yes, there is that niggle, too.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list