[SciPy-User] best way to convert a structured array to a float view (again)
Fri Jun 4 14:40:15 CDT 2010
On Fri, Jun 4, 2010 at 1:38 PM, Vincent Davis <email@example.com> wrote:
> On Fri, Jun 4, 2010 at 9:55 AM, Skipper Seabold <firstname.lastname@example.org> 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)],
>> What I really want to be able to do is something like
> I am going to do some timing but this looks promising. Glad to know I
> am not the onlyone that think going between data types is a hassel.
> arr2 = np.array([(24,4.5),(24,4.5),(24,4.5),(24,4.5),(24,4.5)],
> array([ 1.18575755e-322, 4.50000000e+000, 1.18575755e-322,
> 4.50000000e+000, 1.18575755e-322, 4.50000000e+000,
> 1.18575755e-322, 4.50000000e+000, 1.18575755e-322,
I just relived that that doesn't work for the int part, It really
should give an error.
> Of course if you want to leave arr2 untouched you need some type of copy.
>> But I realize that I can't do this because of how the structs are
>> defined in memory. So my question is, is this the best (cheapest,
>> easiest) way to get arr or arr2 as all floats.
>> arr3 = np.zeros(len(arr), dtype=float)
>> arr3[:] = arr.view(int)
>> arr4 = np.zeros(len(arr2),
>> arr4[:] = arr2[:]
>> arr5 = arr4.view((float,len(arr4.dtype.names)))
>> So now I have arr3 and arr5. I need this to be rather general (can
>> ignore strings and object types for now), so that's the reason for the
>> approach I'm taking here.
>> SciPy-User mailing list
More information about the SciPy-User