# [SciPy-User] best way to convert a structured array to a float view (again)

Vincent Davis vincent@vincentdavis....
Sat Jun 5 10:07:03 CDT 2010

```2010/6/5 Stéfan van der Walt <stefan@sun.ac.za>:
> On 4 June 2010 21:16, Vincent Davis <vincent@vincentdavis.net> wrote:
>> 2010/6/4 Stéfan van der Walt <stefan@sun.ac.za>:
>>> On 4 June 2010 08:55, Skipper Seabold <jsseabold@gmail.com> wrote:
>>>> Say I have the following arrays that I want to view as/cast to plain
>>>> ndarrays with float dtype
>>>>
>>>> import numpy as np
>>>> arr = np.array([(24,),(24,),(24,),(24,),(24,)], dtype=[("var1",int)])
>>>>
>>>> arr2 = np.array([(24,4.5),(24,4.5),(24,4.5),(24,4.5),(24,4.5)],
>>>> dtype=[("var1",int),("var2",float)])
>>
>> How about this,
>> arr2 = np.column_stack((arr[col] for col in arr.dtype.names))
>
> That code is more complicated, and results in a 10x slow-down:
>
> In [5]: timeit np.column_stack((arr[col] for col in arr.dtype.names))
> 100000 loops, best of 3: 13.4 us per loop
>
> In [6]: timeit arr.view(int).astype(float)
> 100000 loops, best of 3: 2.01 us per loop

Yes but what do you do for a 2d array? arr.view(int).astype(float)
doesn't really work for that? I might be solving a different problem
from Skipper.

>>> arr2
array([(24, 4.5), (24, 4.5), (24, 4.5), (24, 4.5), (24, 4.5)],
dtype=[('var1', '<i8'), ('var2', '<f8')])
>>> arr2.view(int).astype(float)
array([  2.40000000e+01,   4.61675257e+18,   2.40000000e+01,
4.61675257e+18,   2.40000000e+01,   4.61675257e+18,
2.40000000e+01,   4.61675257e+18,   2.40000000e+01,
4.61675257e+18])

Vincent

> Regards
> Stéfan
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
```

More information about the SciPy-User mailing list