I must be making some sort of really trivial mistake, but I'm trying to create a structured array with names for a single axis, e.g., I have an array data
with shape (2, 3, 4)
, and I want to name the first axis such that I can access data['a']
and data['b']
in an both cases get (3, 4)
shaped slices. I tried:
shape = (2, 3, 4)
data = np.arange(np.product(shape)).reshape(shape)
dtype = [(nn, float) for nn in ['a', 'b']]
data = np.array(data, dtype=dtype)
But this seems to duplicates all of the data into both 'a' and 'b', e.g.
print(data.shape)
print(data['a'].shape)
> (2, 3, 4)
> (2, 3, 4)
I tried specifying that the shape (in the dtype specification) should be (3, 4)
but that duplicated the data 12 more times... and I tried changing the axes order to (3, 4, 2)
, but that doesn't do anything. Any help appreciated!