# Compute the multiplicative inverse of a matrix in Python

PythonServer Side ProgrammingProgrammingNumpy

#### 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 (multiplicative) inverse of a matrix, use the numpy.linalg.inv() method in Python. Given a square matrix a, return the matrix ainv satisfying dot(a, ainv) = dot(ainv, a) = eye(a.shape). The method returns (Multiplicative) inverse of the matrix a. The 1st parameter, a is a Matrix to be inverted.

## Steps

At first, import the required libraries-

import numpy as np
from numpy.linalg import inv

Create an array −

arr = np.array([[ 5, 10], [ 15, 20 ]])

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 (multiplicative) inverse of a matrix, use the numpy.linalg.inv() method in Python −

print("\nResult...\n",np.linalg.inv(arr))

## Example

import numpy as np
from numpy.linalg import inv

# Create an array
arr = np.array([[ 5, 10], [ 15, 20 ]])

# 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 (multiplicative) inverse of a matrix, use the numpy.linalg.inv() method in Python.
print("\nResult...\n",np.linalg.inv(arr))

## Output

Our Array...
[[ 5 10]
[15 20]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

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

Result...
[[-0.4 0.2]
[ 0.3 -0.1]]