In this Python tutorial, we will learn about the “**Scipy Matri**x” where we will discuss how to perform a variety of different operations on matrices using Scipy. And we will also cover the following topics.

- Python Scipy Matrices
- Python Scipy Matrix Inverse
- Scipy Matrix Exponential
- Python Scipy Matrix Power
- Scipy Matrix Transpose
- Python Scipy Matrix Norm
- Python Scipy Matrix To Array
- Scipy Matrix Square Root

## Python Scipy Matrix

The Python Scipy has two module

and *scipy.linalg*

to perform different types of operations on matrices. But first, we need to know about “What is the matrix?” if you don’t know. A matrix is a two-dimensional array in which each data member has the same size.*scipy.sparse*

For many mathematical and scientific tasks, matrices are essential data structures. Here we’ll use Python to generate a matrix and then perform operations on it.

A Python matrix is a 2-dimensional rectangular array containing data recorded in rows and columns in a customized two-dimensional rectangular array. Numbers, strings, equations, symbols, and other types of data can all be found in a matrix.

Read: Scipy Find Peaks – Useful Tutorial

## Python Scipy Matrix Inverse

The Python SciPy module

contains a method *scipy.linalg*

that calculates a matrix’s inverse.*inv()*

The syntax is given below.

`scipy.linalg.inv(a, check_finite=False, overwrite_a=True,)`

Where parameters are:

Input the matrix that we want to be inverted.*a(array_data):*To overwrite*overwrite_a(boolean):*`a`

.To check if the provided matrix as input has finite numbers.*check_finite(boolean):*

The method

returns the *inv()* of type ndarray.

`ainv`

(which is the inverse of a matrix)Let’s take an example by following the below steps:

Import the required libraries using the below code.

```
from scipy.linalg import inv
import numpy as np
```

Create a matrix and inverse it using the below code.

```
matrix = np.array([[8, 7], [4, 3]])
inv(matrix)
```

This is how to inverse the given matrix using the method

of Python Scipy.*scipy.linalg.inv()*

Read: Scipy Normal Distribution

## Scipy Matrix Exponential

The Python SciPy module

contains a method *scipy.linalg*

that uses Pade approximation to compute the matrix exponential.*expm()*

The syntax is given below.

`scipy.linalg.expm(A)`

Where parameter ** A** accepts the matrix of the type array.

The method

returns exponential of matrix A of type ndarray.*expm()*

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

```
from scipy import linalg
import numpy as np
```

Create a matrix and compute the exponential of that matrix using the below code.

```
mat = np.array([[2.0, 1.0], [3.0, -1.0]])
linalg.expm(1j*mat)
```

This is how to compute the exponential of the given matrix using the method

of Python SciPy.*expm()*

Read: Scipy Ndimage Rotate

## Python Scipy Matrix Power

The Python SciPy has a method

within module *fractional_matrix_power()*

to calculate a matrix’s fractional power.*scipy.linalg*

The syntax is given below.

`scipy.linalg.fractional_matrix_power(A, t)`

Where parameters are:

Input the matrix to evaluate the fractional power.*A(array_data like N, N):*To specify the fraction power for the matrix.*t(float):*

The method

returns *fractional_matrix_power()* of type ndarray.

`x`

(which is fractional power of matrix)Let’s take an example to calculate the fractional power of the matrix by following the below steps:

Import the required libraries using the below code.

```
from scipy import linalg
import numpy as np
```

Create a matrix and calculate the fractional power of that matrix using the below code.

```
matrix = np.array([[3.0, 1.0], [4.0, 1.0]])
linalg.fractional_matrix_power(matrix,1.5)
```

This is how to calculate the fractional power of the matrix using the method

of Python SciPy.*fractional_matrix_power()*

Read: Scipy Signal – Helpful Tutorial

## Python Scipy Matrix Transpose

The matrix can be transposed or reversed using the method `transpose()`

that exists within the module

of Python SciPy.*scipy.sparse.csr_matrix*

The syntax is given below,

`csr_matrix.transpose(axes=None, copy=True)`

Where parameters are:

This argument is only included in the signature for NumPy compatibility. Nothing but the default value should be passed in.*axes(optional, None):*When possible, whether or not self-aspects should be duplicated. Depending on what type of sparse matrix is employed, the extent to which characteristics are transferred varies.*copy(boolean):*

The method

returns the *transpose()*`p`

( which is transposed matrix).

Read: Scipy Convolve

## Python Scipy Matrix Norm

Python SciPy module

contains a method **scipy.linalg**

that returns matrix norms.**norm()**

The syntax is given below.

`scipy.linalg.norm(a, axis=None, ord=None, check_finite=True, keepdims=True)`

Where parameters are:

Inputs array If ord isn’t None, the axis must be None and a must be 1-D or 2-D. If both axis and ord are None, the a.ravel 2-norm will be returned.*a(array_data):*If the axis is an integer, then it indicates the axis the vector norms should be computed along. If the axis is a two-tuple, two-dimensional matrices are held on axes that are specified, and the matrix norms of these matrices are calculated.*axis(2 tuple of int, int):*It is used to provide the norm order.*ord(int, inf):*To check if the provided matrix as input has finite numbers.*check_finite(boolean):*if this is set to true, the axes which are normed over are left in the result as dimensions with size one.*keepdims(boolean):*

The method

returns the norm of the given matrix.*norm()*

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

```
from scipy.linalg import norm
import numpy as np
```

Create an array of data as a matrix whose norm will be computed using the below code.

```
array_data = np.arange(12) - 4.0
array_data
```

Compute the norm of the above-created matrix or array of data using the below code.

`norm(array_data)`

This is how to compute the normalization of a given matrix using the method

of Python SciPy.*norm()*

Read: Scipy Misc + Examples

## Python Scipy Matrix To Array

The Python Scipy module

contains a method *scipy.sparse.csr_matrix*

to convert the given matrix into an array.*toarray()*

The syntax is given below.

`csr_matrix.toarray(order=None, out=None)`

where parameters are:

It is used to specify which orders to use like row-major(C) and column-major(F) for storing the multi-dimensional data. By default, it is specified as*order:*`None`

means no order.It is used to specify how to return the result as an array (NumPy.matrix) like an output buffer instead of creating a new array while returning the result.*out:*

Let’s take an example using the below steps:

Import the necessary libraries using the below code.

```
import numpy as np
from scipy.sparse import csr_matrix
```

Creating arrays using the below code.

```
array_data = np.array([0, 0, 2, 2, 1, 1, 4, 4, 5, 5, 1, 1, 2])
```

Creating `csr matrix`

using the below code.

`csr_mat = csr_matrix(array_data)`

Convert the `csr matrix`

to the NumPy array matrix by applying the method `toarray()`

on the matrix `csr_mat`

using the below code.

`csr_mat.toarray()`

This is how to convert the given matrix using the method

of Python SciPy.*toarray()*

Read: Scipy Integrate + Examples

## Scipy Matrix Square Root

The Python SciPy module

contains a method *scipy.linalg*

that calculates the square root of the given matrix.*sqrtm()*

The syntax is given below.

`scipy.linalg.sqrtm(A, disp=False, blocksize=64)`

Where parameters are:

Input the matrix to evaluate the square root.*A(array_data):*Rather than returning the estimated error, print a warning if the error in the result is predicted to be high.*dist(boolean):*Use a blocked algorithm if the block size is not degenerate in relation to the input array size.*bolcksize(integer):*

The method `sqrtm`

returns the of type ndarray.

`sqrtm`

( which is the square root of the matrix)Let’s understand with an example by following the below steps:

Import the required libraries using the below python code.

```
from scipy import linalg
import numpy as np
```

Create a matrix and calculate the square root of that matrix using the below code.

```
mat = np.array([[2.0, 1.0], [3.0, -1.0]])
linalg.sqrtm(mat)
```

This is how to compute the square root of the given matrix using the method

of Python SciPy.*sqrtm()*

Also, take a look at some more Scipy tutorials.

- Python Scipy Special
- Python Scipy FFT
- Scipy Stats – Complete Guide
- Scipy Sparse – Helpful Tutorial
- Scipy Optimize – Helpful Guide
- Scipy Linalg – Helpful Guide

So, in this tutorial, we have learned about the “**Scipy Matrix**” with the help of a variety of examples. Moreover, we also covered the following topics.

- Python Scipy Matrix
- Python Scipy Matrix Inverse
- Scipy Matrix Exponential
- Python Scipy Matrix Power
- Scipy Matrix Transpose
- Python Scipy Matrix Norm
- Python Scipy Matrix To Array
- Scipy Matrix Square Root

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.