SciPy
- SciPy stands for Scientific Python.
- It is a free and open-source Python library.
- It is used for scientific computing and technical computing. Also, it's used in mathematics, scientific computing, Engineering, and technical computing.
- SciPy contains modules for complex mathematical and technical operations like:- optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers and other tasks common in science and engineering.
- SciPy builds on the NumPy array (to understand NumPy you can refer to- https://www.c-sharpcorner.com/article/getting-started-with-numpy/ and https://www.c-sharpcorner.com/article/numpy-array/ ) object and is part of the NumPy stack.
- It includes tools like Matplotlib, pandas and symPy (library for symbolic mathematics).
SciPy has various sub-packages to perform different mathematical and scientific functions, which are as follows,
SPECIAL FUNCTION PACKAGE: (scipy.special)
This package includes basic mathematical physics’ functions like,
FUNCTION
|
SYNTAX
|
DESCRIPTION
|
Cubic Root
|
scipy.special.cbrt()
|
Finds the cube root of values
|
Exponential
|
scipy.special.exp10()
|
Computes the 10**x element-wise
|
Combinations
|
scipy.special.comb()
|
Calculates the Combination
|
Permutations
|
scipy.special.perm()
|
Calculates the Permutation
|
Log Sum Exponential
|
scipy.special.logsumexp()
|
Computes the log of sum exponential input element.
|
Bessel Function
|
scipy.special.jn()
|
Nth integer order calculation function
|
Relative Error Exponential
|
scipy.special.exprel()
|
It generates the relative error exponential, (exp(x) - 1)/x.
|
Lambert Function W(z)
|
scipy.special.lambertw()
|
The value of W(z) is such that z = W(z) * exp(W(z)) for any complex number z.
|
Gamma Function
|
scipy.special.gamma()
|
The gamma function is often referred to as the generalized factorial.
|
EXAMPLE
- from scipy.special import exprel
- from scipy.special import cbrt
- from scipy.special import exp10
- from scipy.special import lambertw
- from scipy.special import comb
- from scipy.special import perm
- from scipy.special import gamma
-
- cb = cbrt([27, 64, 0.1254])
- print(cb)
-
- exp = exp10([4,10])
- print(exp)
-
- com = comb(5, 2)
- print(com)
-
- per = perm(5, 2)
- print(per)
-
- res = exprel([-0.25, -0.1, 0, 0.1, 0.25])
- print (res)
-
- w = lambertw(1)
- print (w)
-
- gm = gamma([0, 0.5, 1, 5])
- print (gm)
OUTPUT
SCIPY CONSTANT PACKAGE(scipy.constants)
This package provides many constants which we use in daily mathematical problems or SI units or any measuring units.
EXAMPLE
- import scipy.constants
-
- print("sciPy - pi = %.16f"%scipy.constants.pi)
- print("Gravitational constant- G = %.16f"%scipy.constants.G)
- print("sciPy - micro = %.16f"%scipy.constants.micro)
OUTPUT
LINERA ALGEBRA PACKAGE: (scipy.linalg)
Linear algebra block accepts two-dimensional array object and output is also a two-dimensional array.
EXAMPLE
- from scipy import linalg
- import numpy as np
-
- a = np.array([ [4,5], [3,2] ])
-
- linalg.det(a)
-
- linalg.inv(a)
-
- eg_val, eg_vect = linalg.eig(a)
-
- print(eg_val)
- print(eg_vect)
OUTPUT
DICRETE FOURIER TRANSFORMATION PACKAGE:(scipy.fftpack)
Discrete Fourier Transformation is used to convert Spatial data into Frequency data. Fourier Transformation is computed on a time domain signal to check its behavior in the frequency domain. FFT(Fast Fourier Transformation) is an algorithm to implement DFT
EXAMPLE
- import numpy as np
- from scipy.fftpack import fft
-
- x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
- y = fft(x)
- print (y)
OUTPUT
IMAGE PROCESSING PACKAGE:(scipy.misc, scipy,ndimage)
- scipy.ndimage is a submodule of SciPy which is mostly used for performing an image related operation. You can find numerous functions involved in ndimage package you can find these in the attachment- Scipy(ndimage,Misc)
- scipy.misc; MISC is a package which contains prebuilt images which can be used to perform image manipulation task. You can find numerous functions involved in misc package you can find these in the attachment- Scipy(ndimage,Misc)
By using these packages, we can zoom, flip, blurr, rotate, crop, sharpen or add filters to an image ; and many more operations can be done.
EXAMPLE: (Display an image)
- from scipy import misc
- from matplotlib import pyplot as plt
- import numpy as np
-
- panda = misc.face()
- plt.imshow( panda )
- plt.show()
OUTPUT
EXAMPLE: (Flip down the Image)
- from scipy import misc
- from matplotlib import pyplot as plt
- import numpy as np
-
- panda = misc.face()
- flip_down = np.flipud(misc.face())
-
- plt.imshow(flip_down)
- plt.show()
OUTPUT
EXAMPLE: (Rotation of the Image)
- from scipy import misc, ndimage
- from matplotlib import pyplot as plt
- import numpy as np
-
- panda = misc.face()
- panda_rotate = ndimage.rotate(panda, 135)
-
- plt.imshow(panda_rotate)
- plt.show()
OUTPUT
INTEGRATION PACKAGE(scipy.integrate)
This package has single integration, double, triple, multiple, Gaussian quadrate, Romberg, Trapezoidal and Simpson's rules.
EXAMPLE: (Single Integration)
-
- from scipy.integrate import quad
-
- def f(x):
- return x**2
-
- ans, err = quad(f, 0, 1)
- print (ans)
OUTPUT
EXAMPLE - (Double Integration)
- Double integration- we use Lambda function here because we want that function to have to return constants
- For double integration we use, dblquad
- Also order of arguments in the integrand is different than in Matlab.
- from scipy.integrate import dblquad
- import numpy as np
-
- def f(y, x):
- return y * np.sin(x) + x * np.cos(y)
-
-
-
-
- ans, err = dblquad(f, np.pi, 2*np.pi,
- lambda x: 0,
- lambda x: np.pi)
- print (ans)
OUTPUT
INTERPOLATE PACKAGE:(scipy.interpolate)
Interpolation is the process of finding a value between two points on a line or a curve.
EXAMPLE
- import numpy as np
- from scipy import interpolate
- import matplotlib.pyplot as plt
-
- x = np.linspace(0, 4, 12)
- y = np.cos(x**2/3+4)
-
- print (x,y)
- plt.plot(x, y,'o')
- plt.show()
OUTPUT
When we want to interpolate between two fixed points then we use 1D interpolation,
interp1d(x, y,kind = 'linear')
We can also draw mechanical splines using interpolate.
INPUT/OUTPUT PACKAGE:(scipy. io)
This would help us to read or write data in a particular file. Also, we can read a data from file1 and write it in file2 and many more. We would learn more such file handling under the topic CSV.
We have few more packages under SciPy,
- CLUSTER PACKAGE
- These are Compressed sparse graphs.
- Focuses on Fast graph algorithms based on sparse matrix representations.
scipy.sparse.csgraph
- STATS PACKAGE
- All statistics’ functions are found in this package.
- We can calculate harmonic mean, geometric mean, mode, kurtosis, median, skewness, interquartile range etc.
scipy.stats
- CS GRAPH PACKAGE/ SPARSE
- These are Compressed Sparse Graphs.
- It is used to store large sparse matrices and provide the functionality to perform complex matrix computations.
scipy.sparse.csgraph
- ODR PACKAGE
- These are Orthogonal Distance Regressions which are used for regression studies.
- This is to calculate relationship between the two variables y and x by drawing the line of the best fit in the graph
scipy.odr
SUMMARY
We learned what is SciPy and how it is used, and what its sub-packages are. We know how to solve mathematical complex problems through Python now. Also you can now proceed with Scientific and Technical computations using SciPy in Python.
Practice!
Feedback or queries related to this article are most welcome.
Thanks for reading.