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

Stéfan van der Walt stefan at sun.ac.za
Sat Jun 5 04:24:40 EDT 2010


On 4 June 2010 21:16, Vincent Davis <vincent at vincentdavis.net> wrote:
> 2010/6/4 Stéfan van der Walt <stefan at sun.ac.za>:
>> On 4 June 2010 08:55, Skipper Seabold <jsseabold at 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

Regards
Stéfan



More information about the SciPy-User mailing list