Learn (Python) SciPy

SciPy

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
  1. from scipy.special import exprel    
  2. from scipy.special import cbrt    
  3. from scipy.special import exp10    
  4. from scipy.special import lambertw    
  5. from scipy.special import comb    
  6. from scipy.special import perm    
  7. from scipy.special import gamma    
  8.     
  9. cb = cbrt([27640.1254])    
  10. print(cb)    
  11.     
  12. exp = exp10([4,10])    
  13. print(exp)    
  14.     
  15. com = comb(52)    
  16. print(com)    
  17.     
  18. per = perm(52)    
  19. print(per)    
  20.     
  21. res = exprel([-0.25, -0.100.10.25])    
  22. print (res)    
  23.     
  24. w = lambertw(1)    
  25. print (w)    
  26.     
  27. gm = gamma([00.515])    
  28. print (gm)     
OUTPUT
 
Learn (Python) SciPy
 

SCIPY CONSTANT PACKAGE(scipy.constants)

 
This package provides many constants which we use in daily mathematical problems or SI units or any measuring units.
 
Learn (Python) SciPy
 
EXAMPLE
  1. import scipy.constants    
  2.     
  3. print("sciPy - pi = %.16f"%scipy.constants.pi)    
  4. print("Gravitational constant- G = %.16f"%scipy.constants.G)    
  5. print("sciPy - micro = %.16f"%scipy.constants.micro)    
OUTPUT
 
Learn (Python) SciPy
 

LINERA ALGEBRA PACKAGE: (scipy.linalg)

 
Linear algebra block accepts two-dimensional array object and output is also a two-dimensional array.
 
EXAMPLE
  1. from scipy import linalg    
  2. import numpy as np    
  3.     
  4. a = np.array([ [4,5], [3,2] ])    
  5.     
  6. linalg.det(a)    #Calculating determinant of a two-dimensional matrix    
  7.     
  8. linalg.inv(a)    #Calculates the inverse of square matrix    
  9.     
  10. eg_val, eg_vect = linalg.eig(a)    #Calculates the eigenValues and eigenVectors    
  11.     
  12. print(eg_val)    
  13. print(eg_vect)     
OUTPUT
 
Learn (Python) SciPy

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
  1. import numpy as np    
  2. from scipy.fftpack import fft    
  3.     
  4. x = np.array([1.02.01.0, -1.01.5])    
  5. y = fft(x)    
  6. print (y)     
OUTPUT
Learn (Python) SciPy

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.
 
To understand Matplotlib you can refer to my previous article.
 
EXAMPLE: (Display an image)
  1. from scipy import misc    
  2. from matplotlib import pyplot as plt    
  3. import numpy as np    
  4.     
  5. panda = misc.face()    #get face image of panda from misc package    
  6. plt.imshow( panda )     #plot or show image of face    
  7. plt.show()     
OUTPUT
Learn (Python) SciPy
EXAMPLE: (Flip down the Image)
  1. from scipy import misc    
  2. from matplotlib import pyplot as plt    
  3. import numpy as np    
  4.     
  5. panda = misc.face()    
  6. flip_down = np.flipud(misc.face())    
  7.     
  8. plt.imshow(flip_down)    
  9. plt.show()     
OUTPUT
Learn (Python) SciPy
EXAMPLE: (Rotation of the Image)
  1. from scipy import misc, ndimage    
  2. from matplotlib import pyplot as plt    
  3. import numpy as np    
  4.     
  5. panda = misc.face()    
  6. panda_rotate = ndimage.rotate(panda, 135)    
  7.     
  8. plt.imshow(panda_rotate)    
  9. plt.show()     
OUTPUT
Learn (Python) SciPy

INTEGRATION PACKAGE(scipy.integrate)

 
This package has single integration, double, triple, multiple, Gaussian quadrate, Romberg, Trapezoidal and Simpson's rules.
 
To understand functions in Python refer to my previous article.
 
EXAMPLE: (Single Integration)
  1. #single integration with a = 0 & b = 1 on function f(x)    
  2. from scipy.integrate import quad    
  3.     
  4. def f(x):    
  5. return x**2    
  6.     
  7. ans, err = quad(f, 01)    
  8. print (ans)    
OUTPUT
Learn (Python) SciPy
EXAMPLE - (Double Integration)
  1. Double integration- we use Lambda function here because we want that function to have to return constants
  2. For double integration we use, dblquad
  3. Also order of arguments in the integrand is different than in Matlab.
    1. from scipy.integrate import dblquad    
    2. import numpy as np    
    3.     
    4. def f(y, x):    #y must be the first argument, and x the second.    
    5. return y * np.sin(x) + x * np.cos(y)    
    6.     
    7. #First integral is from a=pi and b=2*pi on f(x)    
    8. #Second integral is from a=0 and b=pi on f(y)    
    9.     
    10. ans, err = dblquad(f, np.pi, 2*np.pi,    
    11. lambda x: 0,    
    12. lambda x: np.pi)    
    13. print (ans)   
OUTPUT
Learn (Python) SciPy
 

INTERPOLATE PACKAGE:(scipy.interpolate)

 
Interpolation is the process of finding a value between two points on a line or a curve.
 
EXAMPLE
  1. import numpy as np    
  2. from scipy import interpolate    
  3. import matplotlib.pyplot as plt    
  4.     
  5. x = np.linspace(0412)    
  6. y = np.cos(x**2/3+4)    
  7.     
  8. print (x,y)    
  9. plt.plot(x, y,'o')    
  10. plt.show()    
OUTPUT
Learn (Python) SciPy
 
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.


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles