[Numpy-discussion] Alternative to record array
Pierre GM
pgmdevlist at gmail.com
Mon Dec 29 12:41:47 EST 2008
Jean-Baptiste,
As you stated, everything depends on what you want to do.
If you need to keep the correspondence age<>weight for each entry,
then yes, record arrays, or at least flexible-type arrays, are the
best. (The difference between a recarray and a flexible-type array is
that fields can be accessed by attributes (data.age) or items
(data['age']) with recarrays, but only with items with felxible-type
arrays).
Using your example, you could very well do:
data['age'] += 1
and still keep the correspondence age<>weight.
Your FieldArray class returns an object that is not a ndarray, which
may have some undesired side-effects.
As Ryan noted, flexible-type arrays are usually faster, because they
lack the overhead brought by the possibiity of accessing data by
attributes. So, if you don't mind using the 'access-by-fields' syntax,
you're good to go.
On Dec 29, 2008, at 10:58 AM, Jean-Baptiste Rudant wrote:
> Hello,
>
> I like to use record arrays to access fields by their name, and
> because they are esay to use with pytables. But I think it's not
> very effiicient for what I have to do. Maybe I'm misunderstanding
> something.
>
> Example :
>
> import numpy as np
> age = np.random.randint(0, 99, 10e6)
> weight = np.random.randint(0, 200, 10e6)
> data = np.rec.fromarrays((age, weight), names='age, weight')
> # the kind of operations I do is :
> data.age += data.age + 1
> # but it's far less efficient than doing :
> age += 1
> # because I think the record array stores [(age_0, weight_0) ...
> (age_n, weight_n)]
> # and not [age0 ... age_n] then [weight_0 ... weight_n].
>
> So I think I don't use record arrays for the right purpose. I only
> need something which would make me esasy to manipulate data by
> accessing fields by their name.
>
> Am I wrong ? Is their something in numpy for my purpose ? Do I have
> to implement my own class, with something like :
>
>
> class FieldArray:
> def __init__(self, array_dict):
> self.array_list = array_dict
>
> def __getitem__(self, field):
> return self.array_list[field]
>
> def __setitem__(self, field, value):
> self.array_list[field] = value
>
> my_arrays = {'age': age, 'weight' : weight}
> data = FieldArray(my_arrays)
>
> data['age'] += 1
>
> Thank you for the help,
>
> Jean-Baptiste Rudant
>
>
>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list