[Numpy-discussion] Alternative to record array
Jean-Baptiste Rudant
boogaloojb at yahoo.fr
Mon Dec 29 10:58:09 EST 2008
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20081229/bee2ed4d/attachment.html>
More information about the NumPy-Discussion
mailing list