|
| 1 | +# 3. SciPy |
| 2 | + |
| 3 | +The scipy package contains various toolboxes dedicated to common issues in scientific computing. Its different submodules correspond to different applications, such as interpolation, integration, optimization, image processing, statistics, special functions, etc. |
| 4 | + |
| 5 | +Scipy can be compared to other standard scientific-computing libraries, such as the GSL (GNU Scientific Library for C and C++), or Matlab’s toolboxes. scipy is the core package for scientific routines in Python; it is meant to operate efficiently on numpy arrays, so that numpy and scipy work hand in hand. |
| 6 | + |
| 7 | +**Sub-packages of SciPy:** |
| 8 | + |
| 9 | +- File input/output - scipy.io |
| 10 | +- Special Function - scipy.special |
| 11 | +- Linear Algebra Operation - scipy.linalg |
| 12 | +- Interpolation - scipy.interpolate |
| 13 | +- Optimization and fit - scipy.optimize |
| 14 | +- Statistics and random numbers - scipy.stats |
| 15 | +- Numerical Integration - scipy.integrate |
| 16 | +- Fast Fourier transforms - scipy.fftpack |
| 17 | +- Signal Processing - scipy.signal |
| 18 | +- Image manipulation – scipy.ndimage |
| 19 | + |
| 20 | +#### **Import Syntax** |
| 21 | +*They all depend on numpy, but are mostly independent of each other. The standard way of importing Numpy and these Scipy modules is:* |
| 22 | +```py |
| 23 | +import numpy as np |
| 24 | +from scipy import stats |
| 25 | +``` |
| 26 | + |
| 27 | +### **File Input / Output package:** |
| 28 | +Scipy, I/O package, has a wide range of functions for work with different files format which are Matlab, Arff, Wave, Matrix Market, IDL, NetCDF, TXT, CSV and binary format : |
| 29 | +```py |
| 30 | +import numpy as np |
| 31 | + from scipy import io as sio |
| 32 | + array = np.ones((4, 4)) |
| 33 | + sio.savemat('example.mat', {'ar': array}) |
| 34 | + data = sio.loadmat(‘example.mat', struct_as_record=True) |
| 35 | + data['ar'] |
| 36 | +``` |
| 37 | +Output: |
| 38 | +``` |
| 39 | +array([[ 1., 1., 1., 1.], |
| 40 | + [ 1., 1., 1., 1.], |
| 41 | + [ 1., 1., 1., 1.], |
| 42 | + [ 1., 1., 1., 1.]]) |
| 43 | +``` |
| 44 | +### **Special Function package** |
| 45 | +- scipy.special package contains numerous functions of mathematical physics. |
| 46 | +- SciPy special function includes Cubic Root, Exponential, Log sum Exponential, Lambert, Permutation and Combinations, Gamma, Bessel, hypergeometric, Kelvin, beta, parabolic cylinder, Relative Error Exponential, etc. |
| 47 | + |
| 48 | +Examples : |
| 49 | +- ### **Cubic Root Function:** |
| 50 | +```py |
| 51 | +from scipy.special import cbrt |
| 52 | +scipy.special.cbrt(x) |
| 53 | +cb = cbrt([27, 64]) |
| 54 | +print(cb) |
| 55 | +``` |
| 56 | +Output : |
| 57 | +``` |
| 58 | +array([3., 4.]) |
| 59 | +``` |
| 60 | + |
| 61 | +* ### **Exponential Function:** |
| 62 | + |
| 63 | +```py |
| 64 | +from scipy.special import exp10 |
| 65 | +exp = exp10([1,10]) |
| 66 | +print(exp) |
| 67 | +``` |
| 68 | +Output : |
| 69 | +``` |
| 70 | +[1.e+01 1.e+10] |
| 71 | +``` |
| 72 | +- ### **Permutations & Combinations:** |
| 73 | +```py |
| 74 | +from scipy.special import perm |
| 75 | +per = perm(5, 2, exact = True) |
| 76 | +print(per) |
| 77 | +``` |
| 78 | +Output : |
| 79 | +``` |
| 80 | +20 |
| 81 | +``` |
| 82 | +## **Linear Algebra with SciPy** |
| 83 | + |
| 84 | +- Linear Algebra of SciPy is an implementation of BLAS and ATLAS LAPACK libraries. |
| 85 | +- Performance of Linear Algebra is very fast compared to BLAS and LAPACK. |
| 86 | +- Linear algebra routine accepts two-dimensional array object and output is also a two-dimensional array. |
| 87 | + |
| 88 | + ### **Calculating determinant of a two-dimensional matrix** ### |
| 89 | + |
| 90 | +```py |
| 91 | +from scipy import linalg |
| 92 | +import numpy as np |
| 93 | +#define square matrix |
| 94 | +two_d_array = np.array([ [4,5], [3,2] ]) |
| 95 | +#pass values to det() function |
| 96 | +linalg.det( two_d_array ) |
| 97 | +``` |
| 98 | +Output : |
| 99 | +``` |
| 100 | +-7.0 |
| 101 | +``` |
| 102 | +### **Calculating Inverse Matrix of a two-dimensional matrix** ### |
| 103 | + |
| 104 | +```py |
| 105 | +from scipy import linalg |
| 106 | +import numpy as np |
| 107 | +# define square matrix |
| 108 | +two_d_array = np.array([ [4,5], [3,2] ]) |
| 109 | +#pass value to function inv() |
| 110 | +linalg.inv( two_d_array ) |
| 111 | +``` |
| 112 | +Output : |
| 113 | +``` |
| 114 | +array( [[-0.28571429, 0.71428571], |
| 115 | + [ 0.42857143, -0.57142857]] ) |
| 116 | +``` |
| 117 | + |
| 118 | +### **Calculating Eigenvalues and Eigenvector of a two-dimensional matrix** ### |
| 119 | + |
| 120 | +```py |
| 121 | +from scipy import linalg |
| 122 | +import numpy as np |
| 123 | +#define two dimensional array |
| 124 | +arr = np.array([[5,4],[6,3]]) |
| 125 | +#pass value into function |
| 126 | +eg_val, eg_vect = linalg.eig(arr) |
| 127 | +#get eigenvalues |
| 128 | +print(eg_val) |
| 129 | +#get eigenvectors |
| 130 | +print(eg_vect) |
| 131 | +``` |
| 132 | +Output : |
| 133 | +``` |
| 134 | +[ 9.+0.j -1.+0.j] |
| 135 | +[ [ 0.70710678 -0.5547002 ] |
| 136 | + [ 0.70710678 0.83205029] ] |
| 137 | +``` |
| 138 | + |
| 139 | +### **Optimization and Fit in SciPy – scipy.optimize** |
| 140 | + |
| 141 | +- Optimization provides a useful algorithm for minimization of curve fitting, multidimensional or scalar and root fitting. |
| 142 | +- Let's take an example of a Scalar Function, to find minimum scalar function. |
| 143 | +```py |
| 144 | +import matplotlib.pyplot as plt |
| 145 | +from scipy import optimize |
| 146 | +import numpy as np |
| 147 | + |
| 148 | +def function(a): |
| 149 | + return a*2 + 20 * np.sin(a) |
| 150 | +plt.plot(a, function(a)) |
| 151 | +plt.show() |
| 152 | +#use BFGS algorithm for optimization |
| 153 | +optimize.fmin_bfgs(function, 0) |
| 154 | +``` |
| 155 | +Output : |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + ### **Image Processing with SciPy – scipy.ndimage** ### |
| 161 | +* scipy.ndimage is a submodule of SciPy which is mostly used for performing an image related operation |
| 162 | +ndimage means the "n" dimensional image. |
| 163 | +* SciPy Image Processing provides Geometrics transformation (rotate, crop, flip), image filtering (sharp and de nosing), display image, image segmentation, classification and features extraction. |
| 164 | + |
| 165 | +*MISC Package in SciPy contains prebuilt images which can be used to perform image manipulation task* |
| 166 | + |
| 167 | +Example : Let's import an image from MISC and display it. |
| 168 | +```py |
| 169 | +from scipy import misc |
| 170 | +from matplotlib import pyplot as plt |
| 171 | +import numpy as np |
| 172 | +animal = misc.face() |
| 173 | +plt.imshow( animal ) |
| 174 | +plt.show() |
| 175 | +``` |
| 176 | +Output : |
| 177 | +(Try it yourself) |
| 178 | + |
| 179 | +## References |
| 180 | +- [More on](https://scipy-lectures.org/intro/scipy.html#optimization-and-fit-scipy-optimize) |
0 commit comments