Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct nested numpy record arrays?

The numpy manual mentions use case for numpy.save

Annie Analyst has been using large nested record arrays to represent her statistical data.

Is it possible to have nested records array without dtype=object? If so, how?

like image 745
lumbric Avatar asked Sep 20 '25 22:09

lumbric


1 Answers

Yes, like so:

engine_dt = np.dtype([('volume', float), ('cylinders', int)])
car_dt = np.dtype([('color', int, 3), ('engine', engine_dt)])  # nest the dtypes

cars = np.rec.array([
    ([255, 0, 0], (1.5, 8)),
    ([255, 0, 255], (5, 24)),
], dtype=car_dt)

print(cars.engine.cylinders)
# array([ 8, 24])

The np.dtype function isn't strictly necessary here, but it's usually a good idea, and gives a small speed boost over letting array call it every time.

Note that rec.array is only necessary here to use the .engine notation. If you used a plain np.array, then you'd use cars['engine']['cylinders']

like image 85
Eric Avatar answered Sep 22 '25 11:09

Eric