What is the numpy.linalg.det() Method

The numpy.linalg.det() method is “used to calculate the determinant of the input matrix.”

Syntax

numpy.linalg.det(array)

Parameters

The np.linalg.det() function takes only one argument as a parameter, the array name.

Return Value

It returns the determinant of the given array. The return value will be in the float data type.

Example 1: How does the numpy.linalg.det() method work?

import numpy as np

# This will create a 2D array of shape 2x2 with values 5 to 8
arr = np.arange(5, 9).reshape(2, 2)
print("The array is:\n", arr)
print("Shape of the array is : ", np.shape(arr))

# Now we will print determinant using det() function
print("Determinant of the given array: ", np.linalg.det(arr))

# Verify with the manual caculation
detr = (5*8)-(7*6)
print("Determinant using manual method: ", detr)

Output

The array is:
 [[5 6]
 [7 8]]
Shape of the array is : (2, 2)
Determinant of the given array: -2.000000000000005
Determinant using manual method: -2

Example 2: How to Use np.linalg.det() Method

import numpy as np

# This will create a 2D array of shape 3x3 with values 1 to 9
arr = np.arange(1, 10).reshape(3, 3)
print("The array is:\n", arr)
print("Shape of the array is : ", np.shape(arr))

# Now we will print determinant using det() function
print("Determinant of the given array: ", np.linalg.det(arr))

# Verify with the manual caculation
detr = 1*(5*9 - 6*8) + 2*(4*9 - 6*7) - 3*(4*8 - 5*7)
print("Determinant using manual method: ", detr)

Output

The array is:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
Shape of the array is : (3, 3)
Determinant of the given array: 0.0
Determinant using manual method: -6

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.