# Linear algebra with Scipy module in Python

In this tutorial, you are going to learn about the linalg (linear algebra) which is the sub package of Scipy module in Python. Scipy module is an inbuilt library that will help us to do the scientific computation. It is easy to use and understand. It has the fast computational power and can work on the numpy arrays too.

## Linear algebra with Scipy module

For using the linalg in python, you have to import this module.

from scipy import linalg

So let’s learn Linear algebra with Scipy module in Python with examples.

#### Solving linear equations with Scipy

Linear equations are those equations which when plotted on the graph gives a straight line. The linear equation is of the form a*x+b*y=z .

For ex: We have two equations in the code:

2x+3y=10

4x-y=6

which we solved manually gives us the x=2 and y=2.

The linalg of scipy has the method solve() which computes the equations and gives the value of x and y in an array. The first value in the array is x and the second one is y.

from scipy import linalg # These are the two linear equations # 2x+3y=10 # 4x-y=6 a=[[2, 3], [4, -1]] b=[10, 6] print("The value of x and y is:") print(linalg.solve(a,b))

**Output:-**

The value of x and y is: [2. 2.]

**Note:** While doing matrix multiplication, always keep in mind that both the matrix should follow the rule of multiplication. In the above equation the matrix **b** is of a type 1×2 and **a** is of a type 2×2 matrix. Therefore, both the equations are compatible and the resultant matrix is of a type 1×2.

#### Find inverse of the matrix with Scipy

The inverse of a matrix can only be found when the matrix is a square matrix.

For ex: **arr** is a square matrix of a type 3×3. So, the inverse of the matrix can be found with the help of inv() method.

import scipy.linalg as alg import numpy as np arr=np.array([[1,2,3],[-1,1,3],[3,2,-5]]) print("Inverse of the 3x3 matrix is:") print(alg.inv(arr))

**Output:-**

Inverse of the 3x3 matrix is: [[ 0.61111111 -0.88888889 -0.16666667] [-0.22222222 0.77777778 0.33333333] [ 0.27777778 -0.22222222 -0.16666667]]

#### Find the determinant of the matrix with Scipy

The determinant of the matrix can be found with the help of det() method.

import scipy.linalg as alg import numpy as np arr=np.array([[1,2,3],[-1,1,3],[3,2,-5]]) print(" The determinant of the matrix is:") print(alg.det(arr))

**Output:-**

The determinant of the matrix is: -18.0

#### Find the eigenvalue and eigenvector with Scipy

The eigen value and eigen vector can be found with the help of eig() method. It gives the value of both the parameters. The first value is eigen value and the other one is the eigen vector.

import scipy.linalg as alg import numpy as np arr=np.array([[1,2,3],[-1,1,3],[3,2,-5]]) egval, egvect=linalg.eig(arr) Print("The eigen values are:") print(egval) print("The eigen vectors are:") print(egvect)

**Output:-**

The eigen values are: [-6.69041576+0.j, 2.69041576+0.j, 1. +0.j] The eigen vectors are: [[ 0.24875735, 0.90258683, -0.85714286], [ 0.37990278, 0.16885907, 0.42857143], [-0.89095099, 0.39600962, -0.28571429]]

**Check out the other tutorials on Python:**

## Leave a Reply