INTRODUCTION TO SCIPY

Ravi shankar
5 min readJul 11, 2021

--

SciPy is a scientific computation library that uses NumPy underneath.

SciPy stands for Scientific Python.

It provides more utility functions for optimization, stats and signal processing.

Like NumPy, SciPy is open source so we can use it freely.

SciPy was created by NumPy’s creator Travis Olliphant.

Why Use SciPy?

If SciPy uses NumPy underneath, why can we not just use NumPy?

SciPy has optimized and added functions that are frequently used in NumPy and Data Science.

Which Language is SciPy Written in?

SciPy is predominantly written in Python, but a few segments are written in C.

Where is the SciPy Codebase?

The source code for SciPy is located at this github repository https://github.com/scipy/scipy

Installation

C:\Users\Your Name>pip install scipy

Import SciPy

from scipy import constants

print(constants.liter)

0.001

Data Analysis with SciPy

SciPy is a python library that is useful in solving many mathematical equations and algorithms. It is designed on the top of Numpy library that gives more extension of finding scientific mathematical formulae like Matrix Rank, Inverse, polynomial equations, LU Decomposition, etc. Using its high level functions will significantly reduce the complexity of the code and helps in better analyzing the data. SciPy is an interactive Python session used as a data-processing library that is made to compete with its rivalries such as MATLAB, Octave, R-Lab,etc. It has many user-friendly, efficient and easy-to-use functions that helps to solve problems like numerical integration, interpolation, optimization, linear algebra and statistics.

The benefit of using SciPy library in Python while making ML models is that it also makes a strong programming language available for use in developing less complex programs and applications.

# import numpy library

import numpy as np

A = np.array([[1,2,3],[4,5,6],[7,8,8]])

Linear Algebra

  1. Determinant of a Matrix
  2. # importing linalg function from scipy
  3. from scipy import linalg
  4. # Compute the determinant of a matrix
  5. linalg.det(A)
  • Output : 2.999999999999997
  1. Compute pivoted LU decomposition of a matrix
    LU decomposition is a method that reduce matrix into constituent parts that helps in easier calculation of complex matrix operations. The decomposition methods are also called matrix factorization methods, are base of linear algebra in computers, even for basic operations such as solving systems of linear equations, calculating the inverse, and calculating the determinant of a matrix.
    The decomposition is:
    A = P L U
    where P is a permutation matrix, L lower triangular with unit diagonal elements, and U upper triangular.

P, L, U = linalg.lu(A)

print(P)

print(L)

print(U)

# print LU decomposition

print(np.dot(L,U))

Output : array([[ 0., 1., 0.], [ 0., 0., 1.], [ 1., 0., 0.]]) array([[ 1. , 0. , 0. ], [ 0.14285714, 1. , 0. ], [ 0.57142857, 0.5 , 1. ]]) array([[ 7. , 8. , 8. ], [ 0. , 0.85714286, 1.85714286], [ 0. , 0. , 0.5 ]]) array([[ 7., 8., 8.], [ 1., 2., 3.], [ 4., 5., 6.]])

  1. Eigen values and eigen vectors of this matrix
  2. eigen_values, eigen_vectors = linalg.eig(A)
  3. print(eigen_values)
  4. print(eigen_vectors)

Output : array([ 15.55528261+0.j, -1.41940876+0.j, -0.13587385+0.j]) array([[-0.24043423, -0.67468642, 0.51853459], [-0.54694322, -0.23391616, -0.78895962], [-0.80190056, 0.70005819, 0.32964312]])

  1. Solving systems of linear equations can also be done
  2. v = np.array([[2],[3],[5]])
  3. print(v)
  4. s = linalg.solve(A,v)
  5. print(s)

Output : array([[2], [3], [5]]) array([[-2.33333333], [ 3.66666667], [-1. ]])

Sparse Linear Algebra

SciPy has some routines for computing with sparse and potentially very large matrices. The necessary tools are in the submodule scipy.sparse.
Lets look on how to construct a large sparse matrix:

# import necessary modules

from scipy import sparse

# Row-based linked list sparse matrix

A = sparse.lil_matrix((1000, 1000))

print(A)

A[0,:100] = np.random.rand(100)

A[1,100:200] = A[0,:100]

A.setdiag(np.random.rand(1000))

print(A)

Output :
<1000x1000 sparse matrix of type ''
with 0 stored elements in LInked List format>
<1000x1000 sparse matrix of type ''
with 1199 stored elements in LInked List format>
  1. Linear Algebra for Sparse Matrices

from scipy.sparse import linalg

# Convert this matrix to Compressed Sparse Row format.

A.tocsr()

A = A.tocsr()

b = np.random.rand(1000)

ans = linalg.spsolve(A, b)

# it will print ans array of 1000 size

print(ans)

Output : array([-2.53380006e+03, -1.25513773e+03, 9.14885544e-01, 2.74521543e+00, 5.99942835e-01, 4.57778093e-01, 1.87104209e-01, 2.15228367e+00, 8.78588432e-01, 1.85105721e+03, 1.00842538e+00, 4.33970632e+00, 5.26601699e+00, 2.17572231e-01, 1.79869079e+00, 3.83800946e-01, 2.57817130e-01, 5.18025462e-01, 1.68672669e+00, 3.07971950e+00, 6.20604437e-01, 1.41365890e-01, 3.18167429e-01, 2.06457302e-01, 8.94813817e-01, 5.06084834e+00, 5.00913942e-01, 1.37391305e+00, 2.32081425e+00, 4.98093749e+00, 1.75492222e+00, 3.17278127e-01, 8.50013844e-01, 1.17524493e+00, 1.70173722e+00, ………….))

Integration

When a function is very difficult to integrate analytically, one simply find a solution through numerical integration methods. SciPy has a capability for doing numerical integration also. Scipy has integration methods in scipy.integrate module.

  1. Single Integrals
    The Quad routine is the important function out of SciPy’s integration functions. If integration in over f(x) function where x ranges from a to b, then integral looks like this.
  2. The parameters of quad is scipy.integrate.quad(f, a, b), Where ‘f’ is the function to be integrated. Whereas, ‘a’ and ‘b’ are the lower and upper ranges of x limit. Let us see an example of integrating
  3. over the range of 0 and 1 with respect to dx.
    We will first define the function f(x)=e^(-x²) , this is done using a lambda expression and then use quad routine.

import scipy.integrate

f= lambda x:np.exp(-x**2)

# print results

i = scipy.integrate.quad(f, 0, 1)

print(i)

(0.7468241328124271, 8.291413475940725e-15)

The quad function returns the two values, in which the first number is the value of integral and the second value is the probable error in the value of integral.

  1. Double Integrals
    The parameters of dblquad function is scipy.integrate.dblquad(f, a, b, g, h). Where, ‘f’ is the function to be integrated, ‘a’ and ‘b’ are the lower and upper ranges of the x variable, respectively, while ‘g’ and ‘h’ are the functions that tells the lower and upper limits of y variable.
    As an example, let us perform the double integral of x*y² over x range from 0 to 2 and y ranges from 0 to 1.
  2. We define the functions f, g, and h, using the lambda expressions. Note that even if g and h are constants, as they may be in many cases, they must be defined as functions, as we have done here for the lower limit.

from scipy import integrate

f = lambda y, x: x*y**2

i = integrate.dblquad(f, 0, 2, lambda x: 0, lambda x: 1)

# print the results

print(i)

Output : (0.6666666666666667, 7.401486830834377e-15)

--

--