SciPy Linear Algebra – SciPy Linalg

Free NumPy course with real-time projects Start Now!!

SciPy is one of the fundamental packages for scientific computations. It provides the user with mathematical algorithms and convenient built-in functions. It is handy in the field of data science.

There are modules in SciPy for working with data science, maths, statistics, and also machine learning. We use scipy.linalg modules for working with machine learning concepts. Let us learn more about SciPy Linear Algebra.

SciPy – Linear Algebra

SciPy is a very useful scientific library and has very fast computational power. It has functionalities to perform linear algebraic operations. We perform all these operations on the input object and it gives the array as output.

SciPy has all the features included in the NumPy linear algebra module and some extended functionality.

The linalg module has specific functions for different types of operations.

Linear Equations in SciPy

We can solve the linear equations using the linalg.solve function. We use it to solve the equations automatically and find the values of the unknown variables.

Using the traditional method we can solve it by using the matrices inverse. But we can ease and fasten the calculations with the use of linalg.solve function.

The equations are of the following format:
a*x + b*y = c
3x +5y=20

The function takes two arrays.

The first array consists of coefficients of unknown variables. The other array is the RHS value of the linear equation.
The function then computes the linear equation to determine the unknown variables.

from scipy import linalg
import numpy as np
a = np.array([[3,3,2], [4,1,2], [1,5,4]])
b = np.array([4,4,6])
#The solve function
x = linalg.solve(a, b)
print(x)

Output

[0.36363636 0.18181818 1.18181818]

Determinant in SciPy

In linear algebra, we compute the scalar value of the determinant of a square matrix. It is a certain property of the linear transformation of a matrix. The determinant is given by det(A), det A or |A|.

In the linalg module, we have a dedicated function for finding the determinant of a matrix as the linalg.det()function.

The function takes in a matrix input and gives a scalar set of values as output.

from scipy import linalg
import numpy as np
A = np.array([[5,2,5],[4,1,2],[6,3,6]])
#The determinant function
x = linalg.det(A)
print (x)

Output

5.9999999999999964

Eigenvalues and EigenVectors in SciPy

Eigenvalues and vectors of a matrix of linear equations are a set of non zero vectors that change at ost by a scalar value when we apply the linear transformation. These are also called the characteristic values or roots. The relation between a set of Eigenvalues (λ) and vectors (v) is given by,

Av = λv

scipy.linalg.eig function returns the scalar set of eigenvalues for the matrix.

from scipy import linalg
import numpy as np
A = np.array([[5,5],[8,8]])
#Passing the values to the eigen function
val, vect = linalg.eig(A)
#Eigen values
print(val)
#Eigen vectors
print(vect)
 

Output

[ 0.+0.j 13.+0.j]
[[-0.70710678 -0.52999894]
[ 0.70710678 -0.8479983 ]]

Singular Value Decomposition

Unlike the Eigenvalues and vectors which can compute values only for square matrices, we have the SVD. Standard Value Decomposition is for matrices that are not square in shape.

The scipy.linalg.svd function factorizes the matrix into two unitary matrices and a 1-D array that consists of singular values.

from scipy import linalg
import numpy as np
a = np.array([[5,5],[8,8]]) 
#Passing the values to the eigen function
a,b,c = linalg.svd(a)
print(a,b,c)

Output

[[-0.52999894 -0.8479983 ][-0.8479983 0.52999894]] [1.33416641e+01 1.25607397e-15]

[[-0.70710678 -0.70710678]
[ 0.70710678 -0.70710678]]

Summary

The SciPy sub-package linalg is the most eligible one for performing linear algebra functions. It is very useful and convenient in the fields of mathematics and data science. It can easily perform complicated operations through the use of predefined functions.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *