# [Numpy-discussion] Reductions and binary ops on recarrays...

Fernando Perez fperez.net@gmail....
Thu Jul 30 02:34:53 CDT 2009

```Howdy,

we recently had a discussion about being able to do some common things
like reductions and binary operations on recarrays, and there didn't
seem to be much consensus on it being needed in the core of numpy.

Since  we do actually need this quite pressingly for everyday tasks,
we wrote a very simple version of this today, I'm attaching it here in
case it proves useful to others.

Basically it lets you do reductions and binary operations on record
arrays whose dtype is a simple composite of native ones (example at
the end).  For our needs it's quite useful, so  it may also be to
others.

Cheers,

f

Example:

>>> import recarrutil as ru
>>> dt = np.dtype(dict(names=['x','y'],formats=[float,float]))
>>> x = np.arange(6,dtype=float).reshape(2,3)
>>> y = np.arange(10,16,dtype=float).reshape(2,3)
>>> z = np.empty( (2,3), dt).view(np.recarray)
>>> z.x = x
>>> z.y = y
>>> z
rec.array([[(0.0, 10.0), (1.0, 11.0), (2.0, 12.0)],
[(3.0, 13.0), (4.0, 14.0), (5.0, 15.0)]],
dtype=[('x', '<f8'), ('y', '<f8')])
>>> ru.mean(z)
rec.array((2.5, 12.5),
dtype=[('x', '<f8'), ('y', '<f8')])
>>> ru.mean(z,0)
rec.array([(1.5, 11.5), (2.5, 12.5), (3.5, 13.5)],
dtype=[('x', '<f8'), ('y', '<f8')])
>>> ru.mean(z,1)
rec.array([(1.0, 11.0), (4.0, 14.0)],
dtype=[('x', '<f8'), ('y', '<f8')])
rec.array([[(0.0, 20.0), (2.0, 22.0), (4.0, 24.0)],
[(6.0, 26.0), (8.0, 28.0), (10.0, 30.0)]],
dtype=[('x', '<f8'), ('y', '<f8')])
>>> ru.subtract(z,z)
rec.array([[(0.0, 0.0), (0.0, 0.0), (0.0, 0.0)],
[(0.0, 0.0), (0.0, 0.0), (0.0, 0.0)]],
dtype=[('x', '<f8'), ('y', '<f8')])
-------------- next part --------------
A non-text attachment was scrubbed...
Name: recarrutil.py
Type: text/x-python
Size: 6088 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090730/b62db889/attachment.py
```