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?
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?
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']
You can construct nested arrays the same way you construct nested lists:
nested_list = [['a',1],['b',2],['c',3]]
import numpy as np
nested_array = np.array(nested_list)