Reputation: 1029
Let's say I have a square matrix with 20 lines and 20 columns. Using NumPy, what should I do to transform this matrix into a 1D array with a single line and 400 columns (that is, 20.20 = 400, all in one line)?
So far, I've tried:
1) array = np.ravel(matrix)
2) array = np.squeeze(np.asarray(matrix))
But when I print array
, it's still a square matrix.
Upvotes: 0
Views: 1465
Reputation: 403
Use the reshape method:
array = matrix.reshape((1,400))
.
This works for both Numpy Array and Matrix types.
UPDATE: As sacul noted, matrix.reshape(-1)
is more general in terms of dimensions.
Upvotes: 3