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']
cars has to be properly nested as you (right mix of [] and ()). That can be a source of errors. But when loading from a csv genfromtxt can use a 'flat' list of columns, as long as there's enough data.genfromtxt first creates an array with a flattened version of the dtype, and then returns a view with the nested dtype.photo to the car_dt which is a 2-dimensional array of unknown size and type int? (let's use a black/white image for simplicity)object field to store that array