[Numpy-discussion] another view puzzle

Robert Kern robert.kern at gmail.com
Wed Jun 3 17:09:14 EDT 2009


On Wed, Jun 3, 2009 at 16:06,  <josef.pktd at gmail.com> wrote:
> On Wed, Jun 3, 2009 at 4:58 PM, Robert Kern <robert.kern at gmail.com> wrote:
>> On Wed, Jun 3, 2009 at 15:23,  <josef.pktd at gmail.com> wrote:
>>>>>> import numpy as np
>>>>>> x = np.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')])
>>>
>>>>>> xvm = x.view(np.matrix)
>>>>>> xvm
>>> matrix([[(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')])
>>>>>> xvm*2
>>> matrix([[(0.0, 1.0, 2.0, 3.0, 4.0, 0.0, 1.0, 2.0, 3.0, 4.0),
>>>         (1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0)]], dtype=object)
>>>>>>
>>>
>>> What am I doing wrong?
>>
>> You simply can't do numerical operations on structured arrays. matrix
>> shows this behavior because it replaces * with dot(), and dot(x, 2)
>> upcasts the array to an object array, which happens to represent
>> records as tuples.
>>
>> This has nothing to do with views.
>
> Ok, I didn't know numpy can have structured matrices, I thought
> matrices are simple 2 dimensional animals.

They *are*. Records are atomic items. They do not form an axis.

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