Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Flatten a Matrix using numpy in Python?
In this article, we will show you how to flatten a matrix using the NumPy library in python.
numpy.ndarray.flatten() Function
The numpy module includes a function called numpy.ndarray.flatten() that returns a one-dimensional copy of the array rather than a two-dimensional or multi-dimensional array.
In simple words, we can say that it flattens a matrix to 1-Dimension.
Syntax
ndarray.flatten(order='C')
Parameters
order ? 'C', 'F', 'A', 'K' (optional)
When we set the order parameter to 'C', the array is flattened in row-major order.
When the 'F' is set, the array is flattened in column-major order.
When the order parameter is set to 'A', the array is flattened in column-major order only if 'a' is Fortran contiguous in memory. The 'K' order flattens the array in the same order that the elements appeared in memory. This parameter is set to 'C' by default.
Return Value ? Returns a flattened 1-D matrix
Using flatten() on 2D Matrix
The following program flattens the given input 2-Dimensional matrix to a 1-Dimensional matrix using the flatten() function ?
# importing numpy module with an alias name
import numpy as np
# creating a 2-Dimensional(2x2) numpy matrix
inputMatrix = np.array([[3, 5], [4, 8]])
# printing the input 2D matrix
print("The input numpy matrix:")
print(inputMatrix)
# flattening the 2D matrix to one-dimensional matrix
flattenMatrix = inputMatrix.flatten()
# printing the resultant flattened matrix
print("Resultant flattened matrix:")
print(flattenMatrix)
The input numpy matrix: [[3 5] [4 8]] Resultant flattened matrix: [3 5 4 8]
Using reshape() Function
The following program flattens the given input 4-Dimensional matrix to a 1-Dimensional matrix using reshape() function ?
# importing numpy module with an alias name
import numpy as np
# creating a 4-Dimensional(4x4) numpy matrix
inputMatrix = np.array([[1, 2, 3, 97],
[4, 5, 6, 98],
[7, 8, 9, 99],
[10, 11, 12, 100]])
# printing the input 4D matrix
print("The input numpy matrix:")
print(inputMatrix)
# reshaping the array and flattening the 4D matrix to a one-dimensional matrix
flattenMatrix = np.reshape(inputMatrix, -1)
# printing the resultant flattened matrix
print("Resultant flattened matrix:")
print(flattenMatrix)
The input numpy matrix: [[ 1 2 3 97] [ 4 5 6 98] [ 7 8 9 99] [ 10 11 12 100]] Resultant flattened matrix: [ 1 2 3 97 4 5 6 98 7 8 9 99 10 11 12 100]
Using flatten() on np.matrix Type
The following program flattens the given input matrix created with np.matrix() using the flatten() function ?
# importing NumPy module with an alias name
import numpy as np
# creating a NumPy matrix (4x4 matrix) using matrix() method
inputMatrix = np.matrix('11, 1, 8, 2; 11, 3, 9, 1; 1, 2, 3, 4; 9, 8, 7, 6')
# printing the input 4D matrix
print("The input numpy matrix:")
print(inputMatrix)
# flattening the 4D matrix to one-dimensional matrix
flattenMatrix = inputMatrix.flatten()
# printing the resultant flattened matrix
print("Resultant flattened matrix:")
print(flattenMatrix)
The input numpy matrix: [[11 1 8 2] [11 3 9 1] [ 1 2 3 4] [ 9 8 7 6]] Resultant flattened matrix: [[11 1 8 2 11 3 9 1 1 2 3 4 9 8 7 6]]
Comparison of Methods
| Method | Creates Copy? | Output Type | Best For |
|---|---|---|---|
flatten() |
Yes | 1D array | Simple flattening |
reshape(-1) |
No (view when possible) | 1D array | Memory-efficient |
ravel() |
No (view when possible) | 1D array | Fastest option |
Conclusion
Use flatten() for creating a copy of flattened data, reshape(-1) for memory-efficient flattening, or ravel() for the fastest flattening operation. All methods convert multi-dimensional arrays into 1D arrays.
