 # How to inverse a matrix using NumPy

## How to inverse a matrix using NumPy

The inverse of a matrix is just a reciprocal of the matrix as we do in normal arithmetic for a single number which is used to solve the equations to find the value of unknown variables. The inverse of a matrix is that matrix which when multiplied with the original matrix will give as an identity matrix. The inverse of a matrix exists only if the matrix is non-singular i.e., determinant should not be 0. Using determinant and adjoint, we can easily find the inverse of a square matrix using below formula,

```if det(A) != 0
else
"Inverse doesn't exist"```

#### Matrix Equation where,

A-1: The inverse of matrix A

x: The unknown variable column

B: The solution matrix

#### Inverse of a Matrix using NumPy

Python provides a very easy method to calculate the inverse of a matrix. The function numpy.linalg.inv() which is available in the python NumPy module is used to compute the inverse of a matrix.

Syntax:

numpy.linalg.inv(a)

Parameters:

a: Matrix to be inverted

Returns:

Inverse of the matrix a.

Example 1:

 `# Python program to inverse` `# a matrix using numpy` ` ` `# Import required package` `import` `numpy as np` ` ` `# Taking a 3 * 3 matrix` `A ``=` `np.array([[``6``, ``1``, ``1``],` `              ``[``4``, ``-``2``, ``5``],` `              ``[``2``, ``8``, ``7``]])` ` ` `# Calculating the inverse of the matrix` `print``(np.linalg.inv(A))`

Output:

```[[ 0.17647059 -0.00326797 -0.02287582]
[ 0.05882353 -0.13071895  0.08496732]
[-0.11764706  0.1503268   0.05228758]]```

Example 2:

 `# Python program to inverse` `# a matrix using numpy` ` ` `# Import required package` `import` `numpy as np` ` ` `# Taking a 4 * 4 matrix` `A ``=` `np.array([[``6``, ``1``, ``1``, ``3``],` `              ``[``4``, ``-``2``, ``5``, ``1``],` `              ``[``2``, ``8``, ``7``, ``6``],` `              ``[``3``, ``1``, ``9``, ``7``]])` ` ` `# Calculating the inverse of the matrix` `print``(np.linalg.inv(A))`

Output:

```[[ 0.13368984  0.10695187  0.02139037 -0.09090909]
[-0.00229183  0.02673797  0.14820474 -0.12987013]
[-0.12987013  0.18181818  0.06493506 -0.02597403]
[ 0.11000764 -0.28342246 -0.11382735  0.23376623]]
```

Example 3:

 `# Python program to inverse` `# a matrix using numpy` ` ` `# Import required package` `import` `numpy as np` ` ` `# Inverses of several matrices can` `# be computed at once` `A ``=` `np.array([[[``1.``, ``2.``], [``3.``, ``4.``]],` `              ``[[``1``, ``3``], [``3``, ``5``]]])` ` ` `# Calculating the inverse of the matrix` `print``(np.linalg.inv(A))`

Output:

```[[[-2.    1.  ]
[ 1.5  -0.5 ]]

[[-1.25  0.75]
[ 0.75 -0.25]]]
```

Last Updated on November 8, 2021 by admin

## Calculate the Euclidean distance using NumPyCalculate the Euclidean distance using NumPy

Calculate the Euclidean distance using NumPy In simple terms, Euclidean distance is the shortest between the 2 points irrespective of the dimensions. In this article to find the Euclidean distance, we will use the NumPy library. This library used for manipulating multidimensional

## numpy.mean() in Pythonnumpy.mean() in Python

numpy.mean() in Python numpy.mean(arr, axis = None) : Compute the arithmetic mean (average) of the given data (array elements) along the specified axis. Parameters : arr : [array_like]input array. axis : [int or tuples of int]axis along which we want to calculate the arithmetic

## How to select rows from a dataframe based on column values ?How to select rows from a dataframe based on column values ?

How to select rows from a dataframe based on column values ? The rows of a dataframe can be selected based on conditions as we do use the SQL queries. The various methods to achieve this is explained in this

## Convert Python List to numpy ArraysConvert Python List to numpy Arrays

Convert Python List to numpy Arrays A list in Python is a linear data structure that can hold heterogeneous elements they do not require to be declared and are flexible to shrink and grow. On the other hand, an array

## numpy.zeros() in Pythonnumpy.zeros() in Python

numpy.zeros() in Python The numpy.zeros() function returns a new array of given shape and type, with zeros. Syntax: numpy.zeros(shape, dtype = None, order = 'C') Parameters : shape : integer or sequence of integers order : C_contiguous or F_contiguous C-contiguous order in

## Numpy size() function | PythonNumpy size() function | Python

Numpy size() function | Python In Python, numpy.size() function count the number of elements along a given axis. Syntax: numpy.size(arr, axis=None) Parameters: arr: [array_like] Input data. axis: [int, optional] Axis(x,y,z) along which the elements(rows or columns) are counted. By default, give the total

## Python – math.cos() functionPython – math.cos() function

Python | math.cos() function In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.cos() function returns the cosine of value passed as argument. The value passed in this function should be in

## numpy.percentile() in pythonnumpy.percentile() in python

numpy.percentile() in python numpy.percentile()function used to compute the nth percentile of the given data (array elements) along the specified axis. Syntax : numpy.percentile(arr, n, axis=None, out=None) Parameters : arr :input array. n : percentile value. axis : axis along which we want to