NumPy det()

The determinant of a matrix is a scalar value that provides information about the properties and behavior of the matrix.

The numpy.linalg.det() function is used to compute the determinant of a square matrix.

import numpy as np

# create a 2x2 matrix
matrix1 = np.array([[2, 4], 
                                  [1, 6]])

# compute the determinant
result = np.linalg.det(matrix1)

print(result)  

# Output: 7.999999999999998

det() Syntax

The syntax of det() is:

numpy.linalg.det(matrix)

det() Arguments

The det() method takes the following arguments:

  • matrix - the input matrix for which we want to compute the determinant

det() Return Value

The det() method returns a floating-point number.


Example 1: Determinant of a 3x3 Matrix

import numpy as np

# create a matrix
matrix1 = np.array([[1, 2, 3], 
             		      [4, 5, 1],
             		      [2, 3, 4]])

# find determinant of matrix1
result = np.linalg.det(matrix1)

print(result)

Output

-5.00

Here, we have used the np.linalg.det(matrix1) function to find the determinant of the square matrix matrix1.


Example 2: Determinant of a Random Matrix

import numpy as np

# create a random 2x2 matrix
matrix1 = np.random.randint(0, 10, (2, 2))  

# find determinant of matrix1
result = np.linalg.det(matrix1)

print("Matrix:\n", matrix1)
print("Determinant: \n", result)

Output

Matrix:
[[5 8]
 [2 7]]
Determinant: 
 18.999999999999996

Here, we have created a random 2x2 matrix using np.random.randint() and then used np.linalg.det() to find the determinant.

This code generates a different output each time we run it.