What is SciPy?
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 help to solve problems like numerical integration, interpolation, optimization, linear algebra, and statistics. The benefit of using the SciPy library in Python while making ML models is that it makes a strong programming language available for developing fewer complex programs and applications.
Installation of SciPy
To install SciPy in your system, you can use Python package manager pip. Before proceeding, make sure that you have Python already installed in your system. Here’s the step to install Python in your system.
Step 1: Firstly, Open terminal and Command Prompt in your system.
Step 2: Run the installation Command to install SciPy in your system.
pip install scipy
Step 3: Pip will be download and install SciPy along with dependencies. This process will may take some time depends on internet connection.
Step 4: To verify installation, you need to import SciPy in a Python script or interactive shell.
import scipy
print(scipy.__version__)
Now the installation of SciPy is successfully completed.
How does Data Analysis work with SciPy?
Data Preparation
- Import the necessary libraries: import numpy as np and import scipy as sp.
- Load or generate your dataset using NumPy or pandas.
Exploratory Data Analysis (EDA)
- Use descriptive statistics from SciPy’s stats module to gain insights into the dataset.
- Calculate measures such as mean, median, standard deviation, skewness, kurtosis, etc.
- Python3
from scipy import stats data = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]) # Calculate mean and standard deviation mean_val = np.mean(data) std_dev = np.std(data) # Perform basic statistical tests t_stat, p_value = stats.ttest_1samp(data, popmean = 5 ) print ( "t_stat:" , t_stat) print ( "p_value:" , p_value) |
Output:
1 2 |
t_stat: 0.0 p_value: 1.0 |
Statistical Hypothesis Testing
Use SciPy’s stats module for various hypothesis tests such as t-tests, chi-square tests, ANOVA, etc.
# Example of a t-test
t_stat, p_value = stats.ttest_ind(group1, group2)
Regression Analysis
Utilize the linregress function for linear regression analysis.
# Linear regression
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
Signal and Image Processing
- Use the scipy.signal module for signal processing operations.
- Explore scipy.ndimage for image processing.
from scipy import signal, ndimage
# Example of convolution using signal processing
result = signal.convolve2d(image, kernel, mode=’same’, boundary=’wrap’)
Optimization
Employ the optimization functions in SciPy to find optimal parameter values.
from scipy.optimize import minimize
# Define an objective function
def objective_function(x):
return x[0]**2 + x[1]**2
result = minimize(objective_function, [0, 0])
Import SciPy
Once SciPy is installed , you need to import the SciPy module(s)
- Python3
# import numpy library import numpy as np A = [[ 1 , 2 ,], [ 5 , 6 ,]] |
Linear Algebra
Determinant of a Matrix
- Python3
# importing linalg function from scipy from scipy import linalg A = [[ 1 , 2 ,], [ 5 , 6 ,]] # Compute the determinant of a matrix linalg.det(A) |
Output:
1 |
-4.0 |
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.
- Python3
P, L, U = linalg.lu(A) print (P) print (L) print (U) # print LU decomposition print (np.dot(L,U)) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
[[0. 1. 0.] [0. 0. 1.] [1. 0. 0.]] [[1. 0. 0. ] [0.14285714 1. 0. ] [0.57142857 0.5 1. ]] [[7. 8. 8. ] [0. 0.85714286 1.85714286] [0. 0. 0.5 ]] [[0.14285714 1. 0. ] [0.57142857 0.5 1. ] [1. 0. 0. ]] |
Eigen values and Eigen vectors of this matrix
- Python3
eigen_values, eigen_vectors = linalg.eig(A) print (eigen_values) print (eigen_vectors) |
Output:
1 2 3 4 |
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]]) |
Solving systems of linear equations can also be done
- Python3
v = np.array([[ 2 , 4 ],[ 3 , 8 ]]) print (v) s = linalg.solve(A,v) print (s) |
Output:
1 2 3 4 |
[[2 4] [3 8]] [[-1.5 -2. ] [ 1.75 3. ]] |
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.
Let’s look on how to construct a large sparse matrix
- Python3
# 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
(0, 0) 0.7948113035416484 (0, 1) 0.22210781047073025 (0, 2) 0.1198653673336828 (0, 3) 0.33761517140362796 (0, 4) 0.9429097039125192 (0, 5) 0.32320293202075523 (0, 6) 0.5187906217433661 (0, 7) 0.7030189588951778 (0, 8) 0.363629602379294 (0, 9) 0.9717820827209607 (0, 10) 0.9624472949421112 (0, 11) 0.25178229582536416 (0, 12) 0.49724850589238545 (0, 13) 0.30087830981676966 (0, 14) 0.2848404943774676 (0, 15) 0.036886947354532795 |
Linear Algebra for Sparse Matrices
- Python3
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:
1 2 3 4 5 6 7 8 9 |
[-4.67207136e+01 -3.69332972e+02 3.69393775e-01 6.32141409e-02 3.33772205e+00 5.10104872e-01 3.07850190e+00 1.94608719e+01 1.49997674e+00 1.04751174e+00 9.23616608e-01 8.14103772e-01 8.42662424e-01 2.28221903e+00 4.92361307e+01 6.74574814e-01 3.06515031e-01 3.36481843e-02 9.55613073e-01 7.22911464e-01 2.70518013e+00 1.25039001e+00 1.37825326e-01 3.95005049e-01 4.04480605e+00 7.72817743e-01 2.14200400e-01 7.06283767e-01 1.12635170e-01 5.98880840e+00 4.37382510e-01 8.05571435e-01 ..............................................................................................................................] |
- Python3
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:
1 |
(0.6666666666666667, 7.401486830834377e-15) |
There is a lot more that SciPy is capable of, such as Fourier Transforms, Bessel Functions, etc.
Subpackages in SciPy:
SciPy has a number of subpackages for various scientific computations which are shown in the following table:
However, for a detailed description, you can follow the official documentation.
These packages need to be imported exclusively prior to using them. For example:
1 |
<span id="e57c" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy import cluster</span> |
Before looking at each of these functions in detail, let’s first take a look at the functions that are common both in NumPy and SciPy.
Basic Functions:
Interaction with NumPy:
SciPy builds on NumPy and therefore you can make use of NumPy functions itself to handle arrays. To know in-depth about these functions, you can simply make use of help(), info() or source() functions.
help():
To get information about any function, you can make use of the help() function. There are two ways in which this function can be used:
- without any parameters
- using parameters
Here is an example that shows both of the above methods:
1 2 3 |
<span id="da73" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy import cluster help(cluster) #with parameter help() #without parameter</span> |
When you execute the above code, the first help() returns the information about the cluster submodule. The second help() asks the user to enter the name of any module, keyword, etc for which the user desires to seek information. To stop the execution of this function, simply type ‘quit’ and hit enter.
info():
This function returns information about the desired functions, modules, etc.
1 |
<span id="1d3b" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">scipy.info(cluster)</span> |
source():
The source code is returned only for objects written in Python. This function does not return useful information in case the methods or objects are written in any other language such as C. However in case you want to make use of this function, you can do it as follows:
1 |
<span id="8e6b" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">scipy.source(cluster)</span> |
Special Functions:
SciPy provides a number of special functions that are used in mathematical physics such as elliptic, convenience functions, gamma, beta, etc. To look for all the functions, you can make use of help() function as described earlier.
Exponential and Trigonometric Functions:
SciPy’s Special Function package provides a number of functions through which you can find exponents and solve trigonometric problems.
Consider the following example:
EXAMPLE:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span id="f1e8" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy import special a = special.exp10(3) print(a) b = special.exp2(3) print(b) c = special.sindg(90) print(c) d = special.cosdg(45) print(d)</span> |
OUTPUT:
1000.0
8.0
1.0
0.7071067811865475
There are many other functions present in the special functions package of SciPy that you can try for yourself.
Integration Functions:
SciPy provides a number of functions to solve integrals. Ranging from ordinary differential integrator to using trapezoidal rules to compute integrals, SciPy is a storehouse of functions to solve all types of integrals problems.
General Integration:
SiPy provides a function named quad to calculate the integral of a function which has one variable. The limits can be ( ) to indicate infinite limits. The syntax of the quad() function is as follows:
SYNTAX:
quad(func, a, b, args=(), full_output=0, epsabs=1.49e-08, epsrel=1.49e-08, limit=50, points=None, weight=None, wvar=None, wopts=None, maxp1=50, limlst=50)
Here, the function will be integrated between the limits a and b (can also be infinite).
EXAMPLE:
1 2 3 4 5 |
<span id="be69" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy import special from scipy import integrate a= lambda x:special.exp10(x) b = scipy.integrate.quad(a, 0, 1) print(b)</span> |
In the above example, the function ‘a’ is evaluated between the limits 0, 1. When this code is executed, you will see the following output.
OUTPUT:
(3.9086503371292665, 4.3394735994897923e-14)
Double Integral Function:
SciPy provides dblquad that can be used to calculate double integrals. A double integral, as many of us know, consists of two real variables. The dblquad() function will take the function to be integrated as its parameter along with 4 other variables which define the limits and the functions dy and dx.
EXAMPLE:
1 2 3 4 5 |
<span id="0437" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy import integrate a = lambda y, x: x*y**2 b = lambda x: 1 c = lambda x: -1 integrate.dblquad(a, 0, 2, b, c)</span> |
OUTPUT:
-1.3333333333333335, 1.4802973661668755e-14)
SciPy provides various other functions to evaluate triple integrals, n integrals, Romberg Integrals, etc that you can explore further in detail. To find all the details about the required functions, use the help function.
Optimization Functions:
The scipy.optimize provides a number of commonly used optimization algorithms which can be seen using the help function.
It basically consists of the following:
- Unconstrained and constrained minimization of multivariate scalar functions i.e minimize (eg. BFGS, Newton Conjugate Gradient, Nelder_mead simplex, etc)
- Global optimization routines (eg. differential_evolution, dual_annealing, etc)
- Least-squares minimization and curve fitting (eg. least_squares, curve_fit, etc)
- Scalar univariate functions minimizers and root finders (eg. minimize_scalar and root_scalar)
- Multivariate equation system solvers using algorithms such as hybrid Powell, Levenberg-Marquardt.
Rosenbrook Function:
Rosenbrook function ( rosen) is a test problem used for gradient-based optimization algorithms. It is defined as follows in SciPy:
EXAMPLE:
1 2 3 4 |
<span id="49fc" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">import numpy as np from scipy.optimize import rosen a = 1.2 * np.arange(5) rosen(a)</span> |
OUTPUT: 7371.0399999999945
Nelder-Mead:
The Nelder-Mead method is a numerical method often used to find the min/ max of a function in a multidimensional space. In the following example, the minimize method is used along with the Nelder-Mead algorithm.
EXAMPLE:
1 2 3 4 |
<span id="1782" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy import optimize a = [2.4, 1.7, 3.1, 2.9, 0.2] b = optimize.minimize(optimize.rosen, a, method='Nelder-Mead') b.x</span> |
OUTPUT: array([0.96570182, 0.93255069, 0.86939478, 0.75497872, 0.56793357])
Interpolation Functions:
In the field of numerical analysis, interpolation refers to constructing new data points within a set of known data points. The SciPy library consists of a subpackage named scipy.interpolate that consists of spline functions and classes, one-dimensional and multi-dimensional (univariate and multivariate) interpolation classes, etc.
Univariate Interpolation:
Univariate interpolation is basically an area of curve-fitting which finds the curve that provides an exact fit to a series of two-dimensional data points. SciPy provides interp1d function that can be utilized to produce univariate interpolation.
EXAMPLE:
1 2 3 4 5 6 7 8 |
<span id="764a" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">import matplotlib.pyplot as plt from scipy import interpolate x = np.arange(5, 20) y = np.exp(x/3.0) f = interpolate.interp1d(x, y)x1 = np.arange(6, 12) y1 = f(x1) # use interpolation function returned by `interp1d` plt.plot(x, y, 'o', x1, y1, '--') plt.show()</span> |
OUTPUT:
Multivariate Interpolation:
Multivariate interpolation (spatial interpolation ) is a kind interpolation on functions that consist of more than one variables. The following example demonstrates an example of the
Interpolating over a 2-D grid using the interp2d(x, y, z) function basically will use x, y, z arrays to approximate some function interp2d function. f: “z = f(x, y)” and returns a function whose call method uses spline interpolation to find the value of new points.
EXAMPLE:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span id="e34d" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy import interpolate import matplotlib.pyplot as plt x = np.arange(0,10) y = np.arange(10,25) x1, y1 = np.meshgrid(x, y) z = np.tan(xx+yy) f = interpolate.interp2d(x, y, z, kind='cubic') x2 = np.arange(2,8) y2 = np.arange(15,20) z2 = f(xnew, ynew) plt.plot(x, z[0, :], 'ro-', x2, z2[0, :], '--') plt.show()</span> |
OUTPUT:
Fourier Transform Functions:
Fourier analysis is a method that deals with expressing a function as a sum of periodic components and recovering the signal from those components. The fft functions can be used to return the discrete Fourier transform of a real or complex sequence.
EXAMPLE:
1 2 3 4 |
<span id="4262" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy.fftpack import fft, ifft x = np.array([0,1,2,3]) y = fft(x) print(y)</span> |
OUTPUT: [ 6.+0.j -2.+2.j -2.+0.j -2.-2.j ]
Similarly, you can find the inverse of this by using the ifft function as follows:
EXAMPLE:
1 2 3 4 |
<span id="86da" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">rom scipy.fftpack import fft, ifft x = np.array([0,1,2,3]) y = ifft(x) print(y)</span> |
OUTPUT: [ 1.5+0.j -0.5–0.5j -0.5+0.j -0.5+0.5j]
Signal Processing Functions:
Signal processing deals with analyzing, modifying and synthesizing signals such as sound, images, etc. SciPy provides some functions using which you can design, filter and interpolate one-dimensional and two-dimensional data.
Filtering:
By filtering a signal, you basically remove unwanted components from it. To perform ordered filtering, you can make use of the order_filter function. This function basically performs ordered filtering on an array. The syntax of this function is as follows:
SYNTAX:
order_filter(a, domain, rank)
a = N-dimensional input array
domain = mask array having the same number of dimensions as a
rank = Non-negative number that selects elements from the list after it has been sorted (0 is the smallest followed by 1…)
EXAMPLE:
1 2 3 4 5 |
<span id="a373" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy import signal x = np.arange(35).reshape(7, 5) domain = np.identity(3) print(x,end='nn') print(signal.order_filter(x, domain, 1))</span> |
OUTPUT:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]
[30 31 32 33 34]]
[[ 0. 1. 2. 3. 0.]
[ 5. 6. 7. 8. 3.]
[10. 11. 12. 13. 8.]
[15. 16. 17. 18. 13.]
[20. 21. 22. 23. 18.]
[25. 26. 27. 28. 23.]
[ 0. 25. 26. 27. 28.]]
Waveforms:
The scipy.signal subpackage also consists of various functions that can be used to generate waveforms. One such function is chirp. This function is a frequency-swept cosine generator and the syntax is as follows:
SYNTAX:
chirp(t, f0, t1, f1, method=’linear’, phi=0, vertex_zero=True)
where,
1 2 3 4 5 6 7 8 |
<span id="f0b4" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy.signal import chirp, spectrogram import matplotlib.pyplot as plt t = np.linspace(6, 10, 500) w = chirp(t, f0=4, f1=2, t1=5, method='linear') plt.plot(t, w) plt.title("Linear Chirp") plt.xlabel('time in sec)') plt.show()</span> |
OUTPUT:
Linear Algebra:
Linear algebra deals with linear equations and their representations using vector spaces and matrices. SciPy is built on ATLAS LAPACK and BLAS libraries and is extremely fast in solving problems related to linear algebra. In addition to all the functions from numpy.linalg, scipy.linalg also provides a number of other advanced functions. Also, if numpy.linalg is not used along with ATLAS LAPACK and BLAS support, scipy.linalg is faster than numpy.linalg.
Finding the Inverse of a Matrix:
Mathematically, the inverse of a matrix A is the matrix such that where is the identity matrix consisting of ones down the main diagonal denoted as B=A-1. In SciPy, this inverse can be obtained using the linalg.inv method.
EXAMPLE:
1 2 3 4 5 |
<span id="bbff" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">import numpy as np from scipy import linalg A = np.array([[1,2], [4,3]]) B = linalg.inv(A) print(B)</span> |
OUTPUT:
[[-0.6 0.4]
[ 0.8 -0.2]]
Finding the Determinants:
The value derived arithmetically from the coefficients of the matrix is known as the determinant of a square matrix. In SciPy, this can be done using a function det which has the following syntax:
SYNTAX:
det(a, overwrite_a=False, check_finite=True)
where,
a : (M, M) Is a square matrix
overwrite_a( bool, optional) : Allow overwriting data in a
check_finite ( bool, optional): To check whether the input matrix consists only of finite numbers
EXAMPLE:
1 2 3 4 5 |
<span id="cc4b" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">import numpy as np from scipy import linalg A = np.array([[1,2], [4,3]]) B = linalg.det(A) print(B)</span> |
OUTPUT: -5.0
Sparse Eigenvalues:
Eigenvalues are a specific set of scalars linked with linear equations. The ARPACK provides that allow you to find eigenvalues ( eigenvectors ) quite fast. The complete functionality of ARPACK is packed within two high-level interfaces which are scipy.sparse.linalg.eigs and scipy.sparse.linalg.eigsh. eigs. The eigs interface allows you to find the eigenvalues of real or complex nonsymmetric square matrices whereas the eigsh interface contains interfaces for real-symmetric or complex-hermitian matrices.
The eigh function solves a generalized eigenvalue problem for a complex Hermitian or real symmetric matrix.
EXAMPLE:
1 2 3 4 5 6 |
<span id="20e9" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">from scipy.linalg import eigh import numpy as np A = np.array([[1, 2, 3, 4], [4, 3, 2, 1], [1, 4, 6, 3], [2, 3, 2, 5]]) a, b = eigh(A) print("Selected eigenvalues :", a) print("Complex ndarray :", b)</span> |
OUTPUT:
Selected eigenvalues : [-2.53382695 1.66735639 3.69488657 12.17158399]
Complex ndarray : [[ 0.69205614 0.5829305 0.25682823 -0.33954321]
[-0.68277875 0.46838936 0.03700454 -0.5595134 ]
[ 0.23275694 -0.29164622 -0.72710245 -0.57627139]
[ 0.02637572 -0.59644441 0.63560361 -0.48945525]]
Spatial Data Structures and Algorithms:
Spatial data basically consists of objects that are made up of lines, points, surfaces, etc. The scipy.spatial package of SciPy can compute Voronoi diagrams, triangulations, etc using the Qhull library. It also consists of KDTree implementations for nearest-neighbor point queries.
Delaunay triangulations:
Mathematically, Delaunay triangulations for a set of discrete points in a plane is a triangulation such that no point in the given set of points is inside the circumcircle of any triangle.
EXAMPLE:
1 2 3 4 5 6 7 8 9 |
<span id="d531" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">import matplotlib.pyplot as plt from scipy.spatial import Delaunay points = np.array([[0, 1], [1, 1], [1, 0],[0, 0]]) a = Delaunay(points) #Delaunay object print(a) print(a.simplices) plt.triplot(points[:,0], points[:,1], a.simplices) plt.plot(points[:,1], points[:,0], 'o') plt.show()</span> |
OUTPUT:
Multidimensional Image Processing Functions:
Image processing basically deals with performing operations on an image to retrieve information or to get an enhanced image from the original one. The scipy.ndimage package consists of a number of image processing and analysis functions designed to work with arrays of arbitrary dimensionality.
Convolution and correlation:
SciPy provides a number of functions that allow correlation and convolution of images.
- The function correlate1d can be used to calculate one-dimensional correlation along a given axis
- The function correlate allows multidimensional correlation of any given array with the specified kernel
- The function convolve1d can be used to calculate one-dimensional convolution along a given axis
- The function convolve allows multidimensional convolution of any given array with the specified kernel
EXAMPLE:
1 2 3 |
<span id="cd56" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">import numpy as np from scipy.ndimage import correlate1d correlate1d([3,5,1,7,2,6,9,4], weights=[1,2])</span> |
OUTPUT: array([ 9, 13, 7, 15, 11, 14, 24, 17])
File IO:
The scipy.io package provides a number of functions that help you manage files of different formats such as MATLAB files, IDL files, Matrix Market files, etc.
To make use of this package, you will need to import it as follows:
1 |
<span id="1b3a" class="pk ns fq pg b hk pl pm l ia pn" data-selectable-paragraph="">import scipy.io as sio</span> |
For complete information on subpackage, you can refer to the official document on File IO.
SciPy is an extensive library that offers a wide variety of functions across many different scientific and mathematical domains. Below is a comprehensive list of the primary functions grouped by category. Note that this list will not cover every single function in SciPy but will give you a good overview of the key modules and their functions.
1. Optimization (scipy.optimize
)
scipy.optimize.minimize
: Minimization of scalar or multi-variable functions.scipy.optimize.fmin
: Simplex method to minimize scalar functions.scipy.optimize.fminbound
: Minimization of a scalar function within bounds.scipy.optimize.linprog
: Solves linear programming problems.scipy.optimize.least_squares
: Solves nonlinear least-squares problems.scipy.optimize.root
: Find the roots of a function.scipy.optimize.curve_fit
: Fit a function to data using least squares.scipy.optimize.differential_evolution
: Performs global optimization using differential evolution.scipy.optimize.brute
: Performs a brute-force optimization search over a specified parameter grid.
2. Integration (scipy.integrate
)
scipy.integrate.quad
: Adaptive quadrature for single integrals.scipy.integrate.dblquad
: Adaptive quadrature for double integrals.scipy.integrate.tplquad
: Adaptive quadrature for triple integrals.scipy.integrate.odeint
: Integrates ordinary differential equations.scipy.integrate.simps
: Simpson’s rule for numerical integration.scipy.integrate.trapz
: Trapezoidal rule for numerical integration.scipy.integrate.fixed_quad
: Fixed-order quadrature rule for integration.scipy.integrate.quadrature
: Gauss-Kronrod quadrature for integration.
3. Linear Algebra (scipy.linalg
)
scipy.linalg.solve
: Solve a system of linear equations.scipy.linalg.inv
: Compute the inverse of a matrix.scipy.linalg.det
: Compute the determinant of a matrix.scipy.linalg.eig
: Compute the eigenvalues and eigenvectors of a matrix.scipy.linalg.svd
: Compute the singular value decomposition (SVD) of a matrix.scipy.linalg.lu
: LU decomposition of a matrix.scipy.linalg.qr
: QR decomposition of a matrix.scipy.linalg.cholesky
: Cholesky decomposition of a matrix.scipy.linalg.norm
: Compute the matrix or vector norm.scipy.linalg.pinv
: Compute the Moore-Penrose pseudoinverse of a matrix.
4. Interpolation (scipy.interpolate
)
scipy.interpolate.interp1d
: Interpolate a 1-dimensional function.scipy.interpolate.interp2d
: Interpolate a 2-dimensional function.scipy.interpolate.griddata
: Interpolate scattered data in multiple dimensions.scipy.interpolate.CubicSpline
: Spline interpolation for 1-dimensional data.scipy.interpolate.Rbf
: Radial basis function interpolation.scipy.interpolate.BarycentricInterpolator
: Barycentric Lagrange interpolation.scipy.interpolate.PchipInterpolator
: Piecewise cubic Hermite interpolator.scipy.interpolate.KroghInterpolator
: Interpolate using a polynomial fit.scipy.interpolate.Bessel
: Bessel interpolation (for periodic data).
5. Statistics (scipy.stats
)
scipy.stats.describe
: Return descriptive statistics of data.scipy.stats.mean
: Compute the arithmetic mean of data.scipy.stats.variance
: Compute the variance of data.scipy.stats.skew
: Compute the skewness of data.scipy.stats.kurtosis
: Compute the kurtosis of data.scipy.stats.ttest_ind
: Independent t-test.scipy.stats.ttest_rel
: Paired t-test.scipy.stats.ks_2samp
: Kolmogorov-Smirnov test for two samples.scipy.stats.chisquare
: Chi-squared test.scipy.stats.f_oneway
: One-way ANOVA.scipy.stats.binom
: Binomial distribution.scipy.stats.norm
: Normal (Gaussian) distribution.scipy.stats.poisson
: Poisson distribution.scipy.stats.gamma
: Gamma distribution.scipy.stats.lognorm
: Log-normal distribution.scipy.stats.expon
: Exponential distribution.scipy.stats.mannwhitneyu
: Mann-Whitney U test for comparing two independent samples.scipy.stats.spearmanr
: Spearman’s rank correlation.
6. Signal Processing (scipy.signal
)
scipy.signal.convolve
: Convolution of two signals.scipy.signal.correlate
: Cross-correlation of two signals.scipy.signal.fftconvolve
: Convolution using FFT.scipy.signal.butter
: Design a Butterworth filter.scipy.signal.cheby1
: Design a Chebyshev Type I filter.scipy.signal.cheby2
: Design a Chebyshev Type II filter.scipy.signal.firwin
: Design a Finite Impulse Response (FIR) filter.scipy.signal.sosfilt
: Apply a second-order section (SOS) filter.scipy.signal.find_peaks
: Find peaks in data.scipy.signal.cwt
: Continuous wavelet transform.scipy.signal.resample
: Resample data using Fourier method.scipy.signal.decimate
: Apply a decimation filter to down-sample a signal.
7. Multidimensional Image Processing (scipy.ndimage
)
scipy.ndimage.convolve
: Convolution of multi-dimensional arrays.scipy.ndimage.zoom
: Zoom into multi-dimensional arrays.scipy.ndimage.gaussian_filter
: Apply Gaussian filter to multi-dimensional arrays.scipy.ndimage.binary_dilation
: Binary dilation operation.scipy.ndimage.binary_erosion
: Binary erosion operation.scipy.ndimage.label
: Label connected components in an array.scipy.ndimage.distance_transform_edt
: Compute the Euclidean distance transform.scipy.ndimage.sobel
: Compute the Sobel filter for edge detection.scipy.ndimage.median_filter
: Apply median filter to multi-dimensional data.
8. Sparse Matrices (scipy.sparse
)
scipy.sparse.csr_matrix
: Compressed sparse row matrix format.scipy.sparse.csc_matrix
: Compressed sparse column matrix format.scipy.sparse.dok_matrix
: Dictionary of keys sparse matrix format.scipy.sparse.lil_matrix
: List of lists sparse matrix format.scipy.sparse.eye
: Create a sparse identity matrix.scipy.sparse.random
: Generate sparse random matrices.
9. File I/O (scipy.io
)
scipy.io.loadmat
: Load MATLAB.mat
file.scipy.io.savemat
: Save data to MATLAB.mat
file.scipy.io.mmread
: Read Matrix Market format data.scipy.io.mmwrite
: Write Matrix Market format data.scipy.io.wavfile.read
: Read a WAV file.scipy.io.wavfile.write
: Write to a WAV file.
10. Special Functions (scipy.special
)
scipy.special.gamma
: Gamma function.scipy.special.besselj
: Bessel function of the first kind.scipy.special.ellipe
: Complete elliptic integral of the second kind.scipy.special.erf
: Error function.scipy.special.factorial
: Factorial of a number.scipy.special.lambertw
: Lambert W function.scipy.special.sph_jn
: Spherical Bessel function of the first kind.
11. Cluster Analysis (scipy.cluster
)
scipy.cluster.hierarchy.linkage
: Perform hierarchical/agglomerative clustering.scipy.cluster.hierarchy.dendrogram
: Plot a dendrogram.scipy.cluster.vq.kmeans
: Perform k-means clustering.scipy.cluster.vq.vq
: Quantize data to a codebook (vector quantization).
12. Polynomial Functions (scipy.poly
)
scipy.polyfit
: Polynomial curve fitting.scipy.polyval
: Evaluate a polynomial at specified values.scipy.polynomial.Polynomial
: Class for working with polynomials.
Conclusion
SciPy is an extensive library, and this list includes many of its core functions. Its rich functionality spans optimization, integration, linear algebra, interpolation, statistics, signal processing, image processing, sparse matrices, special functions, and more. These functions are indispensable for solving real-world scientific and mathematical problems, making SciPy a powerful tool for engineers, scientists, data analysts, and machine learning practitioners.