[Numpy-discussion] record arrays initialization

Moroney, Catherine M (388D) Catherine.M.Moroney at jpl.nasa.gov
Wed May 2 19:26:49 EDT 2012



On May 2, 2012, at 3:23 PM, <numpy-discussion-request at scipy.org>
 wrote:

>> A) ?How do I most efficiently construct a record array from a single array?
>> I want to do the following, but it segfaults on me when i try to print b.
>> 
>> vtype = [("x", numpy.ndarray)]
>> a = numpy.arange(0, 16).reshape(4,4)
>> b = numpy.recarray((4), dtype=vtype, buf=a)
> 
> I prefer not to use record arrays, and stick to structured arrays:
> 
> In [11]: vtype = np.dtype([('x', (np.float, 4))])
> 
> In [12]: a = np.arange(16.).reshape((4,4))
> 
> In [13]: a.view(vtype)
> Out[13]:
> array([[([0.0, 1.0, 2.0, 3.0],)],
>       [([4.0, 5.0, 6.0, 7.0],)],
>       [([8.0, 9.0, 10.0, 11.0],)],
>       [([12.0, 13.0, 14.0, 15.0],)]],
>      dtype=[('x', '<f8', (4,))])

Using structured arrays is making my code complex when I try to call the
vectorized function.  If I stick to the original record arrays, what's the 
best way of initializing b from a without doing an row-by-row copy?  

Catherine


More information about the NumPy-Discussion mailing list