Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 152 characters in body
Source Link
unutbu
  • 886.2k
  • 197
  • 1.9k
  • 1.7k

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,) 

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) 

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,) 
Source Link
unutbu
  • 886.2k
  • 197
  • 1.9k
  • 1.7k

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)