Wednesday, 15 February 2012

python - How to hstack arrays of numpy records? -



python - How to hstack arrays of numpy records? -

[an before version of post had inaccurate title "how add together 1 column array of numpy records?" question asked in before title has been partially answered, reply not quite body of before version of post asking for. i've reworded title, , edited post substantially, create distinction clearer. explain why reply mentioned before falls short of i'm looking for.]

suppose have 2 numpy arrays x , y, each consisting of r "record" (aka "structured") arrays. allow shape of x (r, cx) , shape of y (r, cy). let's assume there's no overlap between x.dtype.names , y.dtype.names.

for example, r = 2, cx = 2, , cy = 1:

import numpy np x = np.array(zip((1, 2), (3., 4.)), dtype=[('i', 'i4'), ('f', 'f4')]) y = np.array(zip(('a', 'b')), dtype=[('s', 'a10')])

i "horizontally" concatenate x , y produce new array of records z, having shape (r, cx + cy). operation should not modify x or y @ all.

in general, z = np.hstack((x, y)) won't do, because dtype's in x , y won't match. e.g., continuing illustration above:

z = np.hstack((x, y)) --------------------------------------------------------------------------- typeerror traceback (most recent phone call last) <ipython-input-7-def477e6c8bf> in <module>() ----> 1 z = np.hstack((x, y)) typeerror: invalid type promotion

now, there function, numpy.lib.recfunctions.append_fields, looks may close i'm looking for, have not been able out of it: have tried either fails error, or produces other i'm trying get.

can please show me explicitly code (using n.l.r.append_fields or otherwise1) generate, x , y defined in illustration above, new array of records, z, equivalent horizontal concatenation of x , y, , without modifying either x or y?

i assume require 1 or 2 lines of code. of course, looking code not require building z, record record, iterating on x , y. also, code may assume x , y have same number of records, , there no overlap between x.dtype.names , y.dtype.names. other this, code i'm looking should know nil x , y. ideally, should agnostic number of arrays join. iow, leaving out error checking, code i'm looking body of function hstack_rec new array z result hstack_rec((x, y)).

1...although have admit that, after so-far perfect record of failure numpy.lib.recfunctions.append_fields, i've become bit curious how function used at all, irrespective of relevance post's question.

i never utilize recarrays, , else going come slicker, maybe merge_arrays work?

>>> import numpy.lib.recfunctions nlr >>> x = np.array(zip((1, 2), (3., 4.)), dtype=[('i', 'i4'), ('f', 'f4')]) >>> y = np.array(zip(('a', 'b')), dtype=[('s', 'a10')]) >>> x array([(1, 3.0), (2, 4.0)], dtype=[('i', '<i4'), ('f', '<f4')]) >>> y array([('a',), ('b',)], dtype=[('s', '|s10')]) >>> z = nlr.merge_arrays([x, y], flatten=true) >>> z array([(1, 3.0, 'a'), (2, 4.0, 'b')], dtype=[('i', '<i4'), ('f', '<f4'), ('s', '|s10')])

python numpy recarray

No comments:

Post a Comment