This is similarly worded question as ndim in numpy array loaded with scipy.io.loadmat? - but it's actually a lot more basic.
Say I have this structured array:
import sys
import numpy as np
from pprint import pprint
a = np.array([(1.5,2.5),(3.,4.),(1.,3.)],
dtype=[('x','f4'),('y',np.float32)])
pprint(a)
# array([(1.5, 2.5), (3.0, 4.0), (1.0, 3.0)],
# dtype=[('x', '<f4'), ('y', '<f4')])
I see that as a table of 3 rows and 2 columns, so 3x2. However, trying to use ndim
here, I see:
print(".ndim", a.ndim)
print(".shape", a.shape)
print("asarray.ndim", np.asarray(a).ndim)
# ('.ndim', 1)
# ('.shape', (3,))
# ('asarray.ndim', 1)
... and this is what puzzles me - what makes numpy
think this should be a 1d array, when there are clearly fields/columns defined ?!
Given that output, no wonder that reshape doesn't work:
pprint(a.reshape(3,2))
# ValueError: total size of new array must be unchanged
Now, I can bruteforce the structured array into a ("normal" numpy, I guess?) array:
b = np.column_stack((a['x'], a['y']))
pprint(b)
# array([[ 1.5, 2.5],
# [ 3. , 4. ],
# [ 1. , 3. ]], dtype=float32)
print(".ndim", b.ndim)
print(".shape", b.shape)
print("asarray.ndim", np.asarray(b).ndim)
# ('.ndim', 2)
# ('.shape', (3, 2))
# ('asarray.ndim', 2)
... and so I get the information that I expect.
But I am wondering - why does numpy behave like this with structured arrays - and is there a way to retrieve the 3x2 shape information from the original structured array (a
) directly, without "casting" to a "normal" array?