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

We can convert the type of matrix using the matrix.astype() method of the NumPy module, but there is a problem with data loss. For example, if we wish to convert a float to an int, some of the data will be lost. This approach helps in matrix-type conversion.

Syntax:

 matrix.astype()

Return Value:

The matrix after type conversion is returned by the astype() function.

Numpy matrix.astype() 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 astype() function on the given matrix by passing the datatype as an argument to it to convert all the elements of a given matrix to the specified datatype.
  • Store it in another variable.
  • Print the given matrix after type conversion.
  • 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('[2.3, 1.5; 6.8, 3]')
            
# Apply astype() function on the given matrix by passing the datatype as an argument to it 
# to convert all the elements of a given matrix to the specified datatype.
# Store it in another variable
rslt = gvn_matrx.astype(int)
# Print the given matrix after type conversion
print("The given matrix after type conversion:")
print(rslt)

Output:

The given matrix after type conversion:
[[2 1]
 [6 3]]

Explanation:

Here it converts the datatype of all the elements of the given matrix 
to an integer

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 astype() function on the given matrix by passing the datatype as an argument to it to convert all the elements of a given matrix to the specified datatype.
  • Store it in another variable.
  • Print the given matrix after type conversion.
  • 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.2, 4, 10.3; 8.8, 7, 3.3; 10.1, 9.9, 5.]')
            
# Apply astype() function on the given matrix by passing the datatype as an argument to it 
# to convert all the elements of a given matrix to the specified datatype.
# Store it in another variable
rslt = gvn_matrx.astype(int)
# Print the given matrix after type conversion
print("The given matrix after type conversion:")
print(rslt)

Output:

The given matrix after type conversion:
[[ 2 4 10]
 [ 8 7 3]
 [10 9 5]]