[Numpy-discussion] another view puzzle

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Jun 3 17:06:59 EDT 2009


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. But I haven't looked at
them much.

Josef

> --
> Robert Kern



More information about the NumPy-Discussion mailing list