Can NumPy's flatten() return a 2D array?
I applied flatten()
on an array of shape (12L, 54L)
, and I got a new array of shape (1L, 648L)
. Is that supposed to happen from time to time? If so, in which cases does that happen?
I use NumPy 1.9.2.
Can NumPy's flatten() return a 2D array?
I applied flatten()
on an array of shape (12L, 54L)
, and I got a new array of shape (1L, 648L)
. Is that supposed to happen from time to time? If so, in which cases does that happen?
I use NumPy 1.9.2.
Unlike NumPy arrays, NumPy matrices are always 2D objects. So calling flatten
on a NumPy matrix returns another 2D matrix, albeit one with shape (1, N)
:
In [112]: x = np.matrix(np.random.randint(10, size=(12,54)))
In [116]: x.shape
Out[116]: (12, 54)
In [117]: x.flatten().shape
Out[117]: (1, 648)
If you convert the matrix to an array, then flatten
will return a 1D array:
In [125]: np.asarray(x).flatten().shape
Out[125]: (648,)