# Compute the condition number of a matrix in linear algebra in Python

PythonNumpyServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

To compute the condition number of a matrix in linear algebra, use the numpy.linalg.cond() method in Python. This method is capable of returning the condition number using one of seven different norms, depending on the value of p.

Returns the condition number of the matrix. May be infinite. The condition number of x is defined as the norm of x times the norm of the inverse of x; the norm can be the usual L2-norm or one of a number of other matrix norms. The 1st parameter is x, the matrix whose condition number is sought. The 2nd parameter is p, the Order of the norm used in the condition number computation.

## Steps

At first, import the required libraries −

import numpy as np
from numpy import linalg as LA

Create an array −

arr = np.array([[ 1, 1, 0],
[1, 0, 1],
[1, 0, 0]])

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Get the Shape −

print("\nShape of our Array object...\n",arr.shape)

To compute the condition number of a matrix in linear algebra, use the numpy.linalg.cond() method in Python −

print("\nResult...\n",LA.cond(arr))

## Example

import numpy as np
from numpy import linalg as LA

# Create an array
arr = np.array([[ 1, 1, 0],
[1, 0, 1],
[1, 0, 0]])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# To compute the condition number of a matrix in linear algebra, use the numpy.linalg.cond() method in Python
print("\nResult...\n",LA.cond(arr))

## Output

Our Array...
[[1 1 0]
[1 0 1]
[1 0 0]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

Shape of our Array object...
(3, 3)

Result...
3.7320508075688776


Updated on 02-Mar-2022 10:38:17

Advertisements