Python Numpy matrix.flatten() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it in our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.flatten() Function:

We can flatten a given matrix using the matrix.flatten() method of the Numpy module, which returns a one-dimensional matrix as an output.

Syntax:

 matrix.flatten()

Return Value:

The flattened 1-Dimensional matrix from a given matrix is returned by the flatten() function.

What is a Flattened matrix?

Flattening a matrix means flattening the given n-dimensional matrix to a one-Dimensional(1D) matrix.

Example:

Let the matrix be:

[[ 1 2 3]
 [ 4 5 6]
 [ 7 8 9]]

Matrix after Flattening:

[1 2 3 4 5 6 7 8 9]

Numpy matrix.flatten() Function in Python

For 2-Dimensional (2D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
  • Apply flatten() function on the given matrix to get the flattened matrix from a given matrix.
  • Here, it flattens to a 1-Dimensional matrix.
  • Store it in another variable
  • Print the flattened matrix from a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing 
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[3, 2; 4, 5]')
            
# Apply flatten() function on the given matrix to get the flattened matrix from a given matrix.
# Here, it flattens to a 1-Dimensional matrix.
# Store it in another variable
rslt = gvn_matrx.flatten()
# Print the flattened matrix from a given matrix
print("The flattened matrix from a given matrix:")
print(rslt)

Output:

The flattened matrix from a given matrix:
[[3 2 4 5]]

For 3-Dimensional (3D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable
  • Apply flatten() function on the given matrix to get the flattened matrix from a given matrix.
  • Here, it flattens to a 1-Dimensional matrix.
  • Store it in another variable
  • Print the flattened matrix from a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')

# Apply flatten() function on the given matrix to get the flattened matrix from a given matrix.
# Here, it flattens to a 1-Dimensional matrix.
# Store it in another variable
rslt = gvn_matrx.flatten()
# Print the flattened matrix from a given matrix
print("The flattened matrix from a given matrix:")
print(rslt)

Output:

The flattened matrix from a given matrix:
[[ 2 4 1 8 7 3 10 9 5]]