Python Numpy matrix.round() 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 into 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.round() Function:

We can round off the values of the specified matrix using the matrix.round() function of the Numpy module.

Syntax:

 matrix.round()

Return Value:

The rounded values of a given matrix are returned by the round() function.

Numpy matrix.round() 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
  • Print the given matrix
  • Apply the round() function on the given matrix to round off the values of the given matrix and store it in another variable.
  • Print the given matrix after rounding off the values.
  • 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('[10.3, 1.9; 2.5, 7.79]')

# Print the given matrix
print("The given matrix is:") 
print(gvn_matrx)   

# Apply the round() function on the given matrix to round off the values
# of the given matrix and store it in another variable
rslt = gvn_matrx.round()

# Print the given matrix after rounding off the values
print("The given matrix after rounding off the values:")
print(rslt)

Output:

The given matrix is:
[[10.3 1.9 ]
 [ 2.5 7.79]]
The given matrix after rounding off the values:
[[10. 2.]
 [ 2. 8.]]

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
  • Print the given matrix
  • Apply the round() function on the given matrix to round off the values of the given matrix and store it in another variable.
  • Print the given matrix after rounding off the values.
  • 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('[5.3, 6, 4.6; 2.8, 7.79, 8.1; 9.99, 2, 1.02]')

# Print the given matrix
print("The given matrix is:") 
print(gvn_matrx)   

# Apply the round() function on the given matrix to round off the values
# of the given matrix and store it in another variable
rslt = gvn_matrx.round()

# Print the given matrix after rounding off the values
print("The given matrix after rounding off the values:")
print(rslt)

Output:

The given matrix is:
[[5.3 6. 4.6 ]
 [2.8 7.79 8.1 ]
 [9.99 2. 1.02]]
The given matrix after rounding off the values:
[[ 5. 6. 5.]
 [ 3. 8. 8.]
 [10. 2. 1.]]