How to Calculate the determinant of a matrix using NumPy?
A special number that can be calculated from a square matrix is known as the Determinant of a square matrix. The Numpy provides us the feature to calculate the determinant of a square matrix using numpy.linalg.det() function.
Syntax:
numpy.linalg.det(array)
Example 1: Calculating Determinant of a 2X2 Numpy matrix using numpy.linalg.det() function
Python3
# importing Numpy packageimport numpy as np # creating a 2X2 Numpy matrixn_array = np.array([[50, 29], [30, 44]]) # Displaying the Matrixprint("Numpy Matrix is:")print(n_array) # calculating the determinant of matrixdet = np.linalg.det(n_array) print("\nDeterminant of given 2X2 matrix:")print(int(det)) |
Output:

In the above example, we calculate the Determinant of the 2X2 square matrix.
Example 2: Calculating Determinant of a 3X3 Numpy matrix using numpy.linalg.det() function
Python3
# importing Numpy packageimport numpy as np # creating a 3X3 Numpy matrixn_array = np.array([[55, 25, 15], [30, 44, 2], [11, 45, 77]]) # Displaying the Matrixprint("Numpy Matrix is:")print(n_array) # calculating the determinant of matrixdet = np.linalg.det(n_array) print("\nDeterminant of given 3X3 square matrix:")print(int(det)) |
Output:

In the above example, we calculate the Determinant of the 3X3 square matrix.
Example 3: Calculating Determinant of a 5X5 Numpy matrix using numpy.linalg.det() function
Python3
# importing Numpy packageimport numpy as np # creating a 5X5 Numpy matrixn_array = np.array([[5, 2, 1, 4, 6], [9, 4, 2, 5, 2], [11, 5, 7, 3, 9], [5, 6, 6, 7, 2], [7, 5, 9, 3, 3]]) # Displaying the Matrixprint("Numpy Matrix is:")print(n_array) # calculating the determinant of matrixdet = np.linalg.det(n_array) print("\nDeterminant of given 5X5 square matrix:")print(int(det)) |
Output:

In the above example, we calculate the Determinant of the 5X5 square matrix.



Please Login to comment...