[SciPy-User] masked recarray, recarray with one field of type "ndarray"

Gustavo Goretkin gustavo.goretkin at gmail.com
Wed Feb 1 11:36:38 EST 2012


Thanks for the help! Now is there any way to mask elements of a
recarray? I should explain the application because I think I may be
going about this the wrong way:
I'll be building a tree and each node will have some attributes (for
example, a matrix). I often have to iterate through every node of the
tree and do a calculation -- something that I could do in a vectorized
way with NumPy if all the attributes were stored in an array. So I
thought I could represent the tree as a recarray (that I'd
occasionally need to grow).

I'd also need to delete nodes from the tree occasionally. I'd
accomplish this by masking entries of the recarray. When I needed to
add a node to the tree, I'd try to populate a masked entry before
going to the end of the array.

On Tue, Jan 31, 2012 at 9:33 AM, Warren Weckesser
<warren.weckesser at enthought.com> wrote:
>
>
> On Tue, Jan 31, 2012 at 2:36 AM, Gustavo Goretkin
> <gustavo.goretkin at gmail.com> wrote:
>>
>> Does a recarray support masking?
>>
>> Can I have a recarray where one of the fields is an M-by-N ndarray
>> (not recarray) of some dtype?
>> ex: a = np.recarray(shape=(10),formats=['i4','f8','3-by-3 ndarray of
>> dtype=float64'])
>
>
>
> Here's how it can be done with the dtype argument (in this case, the
> "sub-arrays" are 3x5 float32):
>
> In [21]: dt = np.dtype([('id', int32), ('values', float32, (3,5))])
>
> In [22]: a = np.recarray(shape=(3,), dtype=dt)
>
> In [23]: a.id
> Out[23]: array([      7, 2345536, 8585218])
>
> In [24]: a[0].id
> Out[24]: 7
>
> In [25]: a[0].values
> Out[25]:
> array([[  9.80908925e-45,   2.15997513e-37,   3.16079124e-39,
>           1.18408375e-38,   2.81552923e-38],
>        [  2.13004362e-37,  -7.69011974e-02,   9.80908925e-45,
>           9.80908925e-45,   3.62636667e-21],
>        [  5.67059093e-24,   5.67095065e-24,   5.64768872e-24,
>           7.86448908e+11,   0.00000000e+00]], dtype=float32)
>
> In [26]: a[0].values.shape
> Out[26]: (3, 5)
>
>
> Warren
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list