[Numpy-discussion] converting standard array to record array

josef.pktd at gmail.com josef.pktd at gmail.com
Tue Aug 30 23:49:54 EDT 2011


On Tue, Aug 30, 2011 at 4:34 PM, Bryce Ready <bryce.ready at gmail.com> wrote:
> Hello all,
>
> So i'm using numpy 1.6.0, and trying to convert a (4,4) numpy array of dtype
> 'f8' into a record array of this dtype:
>
>> dt = np.dtype([('mat','(4,4)f8')])
>
> Here is the code snippet:
>
>> In [21]: a = np.random.randn(4,4)
>>
>> In [22]: a.view(dt)
>
> and the resulting error:
>
>> ValueError: new type not compatible with array.
>
> Can anyone shed some light for me on why this conversion is not possible?
> It is certainly technically possible, since the memory layout of the two
> arrays should be the same.
>
> Can anyone recommend a better way to do this conversion?

I guess it can only convert rows, each row needs the memory size of the dt

>>> np.random.randn(4,4).ravel().view(dt).shape
(1,)
>>> np.random.randn(2,4,4).reshape(-1,16).view(dt)
array([[ ([[1.7107996212005496, 0.64334162481360346,
-2.1589367225479004, 1.2302260107072134], [0.90703092017458831,
-1.0297890301610224, -0.095086304368665275, 0.35407366904038495],
[-1.1083969421298907, 0.83307347286837752, 0.39886399402076494,
0.26313136034262563], [0.81860729029038914, -1.1443047382313905,
0.73326737255810859, 0.34482475392499168]],)],
       [ ([[0.69027418489768777, 0.25867753263599164,
1.0320990807184023, 0.21836691513066409], [0.45913017094388614,
-0.89570247025515981, 0.76452726059163534, -2.2953009964941642],
[0.60248580944596275, 1.0863090037733505, -0.10849220482850662,
-0.19176089514256078], [-1.0700600508627109, -1.4743316703511105,
0.79193567523155062, 0.82243321942810521]],)]],
      dtype=[('mat', '<f8', (4, 4))])

Josef

>
> Thanks in advance!
>
> -Bryce Ready
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>



More information about the NumPy-Discussion mailing list