SciPy (Scientific Python) is a Python library used for scientific and technical computing. It builds on NumPy and provides additional functionality for optimization, integration, interpolation, eigenvalue problems, algebraic equations, differential equations, statistics, and many other classes of problems.
| Module | Description |
|---|---|
scipy.cluster |
Clustering algorithms |
scipy.constants |
Physical and mathematical constants |
scipy.fft |
Fast Fourier Transform routines |
scipy.integrate |
Integration and ordinary differential equation solvers |
scipy.interpolate |
Interpolation and smoothing splines |
scipy.io |
Data input and output |
scipy.linalg |
Linear algebra operations |
scipy.ndimage |
N-dimensional image processing |
scipy.optimize |
Optimization and root-finding algorithms |
scipy.signal |
Signal processing |
scipy.sparse |
Sparse matrices and algorithms |
scipy.spatial |
Spatial data structures and algorithms |
scipy.special |
Special functions |
scipy.stats |
Statistical distributions and functions |
| Function | Description | Example |
|---|---|---|
root |
General root finding | from scipy.optimize import rootresult = root(lambda x: x**3 - 1, 0.5)print(result.x) # [1.0] |
fsolve |
Find roots of a function | from scipy.optimize import fsolveresult = fsolve(lambda x: x**2 - 4, 1)print(result) # [2.] |
newton |
Newton-Raphson method | from scipy.optimize import newtonresult = newton(lambda x: x**2 - 2, 1)print(result) # 1.4142135623730951 |
| Function | Description | Example |
|---|---|---|
minimize |
Unified interface for minimization | from scipy.optimize import minimizeresult = minimize(lambda x: x**2 + 2*x + 2, 0)print(result.x) # [-1.] |
minimize_scalar |
Scalar function minimization | from scipy.optimize import minimize_scalarresult = minimize_scalar(lambda x: x**2 + 2*x + 2)print(result.x) # -1.0 |
differential_evolution |
Global optimization | from scipy.optimize import differential_evolutionresult = differential_evolution(lambda x: x[0]**2, [(-10, 10)])print(result.x) # [~0] |
| Function | Description | Example |
|---|---|---|
curve_fit |
Non-linear least squares | from scipy.optimize import curve_fitdef f(x, a, b): return a * x + bparams, _ = curve_fit(f, [0, 1, 2], [1, 3, 5])print(params) # [2. 1.] |
| Function | Description | Example |
|---|---|---|
quad |
Single integration | from scipy.integrate import quadresult, error = quad(lambda x: x**2, 0, 1)print(result) # 0.33333333333333337 |
dblquad |
Double integration | from scipy.integrate import dblquadresult, error = dblquad(lambda y, x: x*y, 0, 1, lambda x: 0, lambda x: 1)print(result) # 0.25 |
odeint |
ODE solver (legacy) | from scipy.integrate import odeintdef model(y, t): return -0.5 * yt = [0, 1, 2]result = odeint(model, 1, t)print(result) # [[1.], [0.60653066], [0.36787944]] |
solve_ivp |
Modern ODE solver | from scipy.integrate import solve_ivpdef model(t, y): return [-0.5 * y[0]]result = solve_ivp(model, [0, 2], [1])print(result.y) # [[1. ... 0.36787944]] |
| Function | Description | Example |
|---|---|---|
interp1d |
1D interpolation | from scipy.interpolate import interp1dx = [0, 1, 2]y = [0, 1, 4]f = interp1d(x, y)print(f(1.5)) # 2.5 |
CubicSpline |
Cubic spline | from scipy.interpolate import CubicSplinex = [0, 1, 2]y = [0, 1, 4]cs = CubicSpline(x, y)print(cs(1.5)) # 2.125 |
griddata |
Interpolate unstructured data | from scipy.interpolate import griddataimport numpy as nppoints = np.array([[0, 0], [1, 0], [0, 1]])values = np.array([1, 2, 3])result = griddata(points, values, (0.5, 0.5), method='linear')print(result) # 2.0 |
| Function | Description | Example |
|---|---|---|
inv |
Matrix inverse | from scipy.linalg import invimport numpy as npA = np.array([[1, 2], [3, 4]])A_inv = inv(A)print(A_inv) # [[-2. 1. ] [ 1.5 -0.5]] |
solve |
Solve linear system | from scipy.linalg import solveA = np.array([[1, 2], [3, 4]])b = np.array([5, 6])x = solve(A, b)print(x) # [-4. 4.5] |
eig |
Eigenvalues/eigenvectors | from scipy.linalg import eigA = np.array([[1, 2], [3, 4]])eigenvals, eigenvecs = eig(A)print(eigenvals) # [-0.37228132 5.37228132] |
svd |
Singular value decomposition | from scipy.linalg import svdA = np.array([[1, 2], [3, 4]])U, s, Vh = svd(A)print(s) # [5.4649857 0.36596619] |
cholesky |
Cholesky decomposition | from scipy.linalg import choleskyA = np.array([[4, 2], [2, 5]])L = cholesky(A, lower=True)print(L) # [[2. 0.] [1. 2.]] |
| Distribution | Description | Example |
|---|---|---|
norm |
Normal distribution | from scipy.stats import normprint(norm.pdf(0)) # 0.3989422804014327print(norm.cdf(1.96)) # 0.9750021048517795 |
uniform |
Uniform distribution | from scipy.stats import uniformprint(uniform.rvs(size=5)) # [Array of 5 random samples] |
t |
Student's t-distribution | from scipy.stats import tprint(t.ppf(0.975, df=10)) # 2.2281388519649385 |
| Function | Description | Example |
|---|---|---|
ttest_ind |
t-test for independent samples | from scipy.stats import ttest_inda = [1, 2, 3, 4, 5]b = [2, 3, 4, 5, 6]t_stat, p_val = ttest_ind(a, b)print(p_val) # 0.1372785765621116 |
pearsonr |
Pearson correlation | from scipy.stats import pearsonrx = [1, 2, 3, 4, 5]y = [2, 3, 5, 8, 9]r, p = pearsonr(x, y)print(r) # 0.9684134781954834 |
linregress |
Linear regression | from scipy.stats import linregressx = [1, 2, 3, 4, 5]y = [2, 4, 5, 4, 6]result = linregress(x, y)print(result.slope) # 0.8 |
| Function | Description | Example |
|---|---|---|
convolve |
Convolution | from scipy.signal import convolvex = [1, 2, 3]y = [0, 1, 0.5]result = convolve(x, y, mode='full')print(result) # [0. 1. 2.5 3.5 1.5] |
filtfilt |
Zero-phase filtering | from scipy.signal import filtfilt, butterb, a = butter(4, 0.2)x = np.random.randn(100)y = filtfilt(b, a, x) |
butter |
Butterworth filter design | from scipy.signal import butterb, a = butter(4, 0.2)print(b[:3]) # [0.0048, 0.0193, 0.0290] |
spectrogram |
Compute spectrogram | from scipy.signal import spectrogramfs = 10e3x = np.random.randn(8000)f, t, Sxx = spectrogram(x, fs) |
| Function | Description | Example |
|---|---|---|
fft |
Fast Fourier Transform | from scipy.fft import fftx = np.array([1, 2, 1, 0])y = fft(x)print(abs(y)) # [4. 1.41421356 0. 1.41421356] |
ifft |
Inverse FFT | from scipy.fft import iffty = np.array([4, 1+1j, 0, 1-1j])x = ifft(y)print(x) # [1.+0.j 2.+0.j 1.+0.j 0.+0.j] |
fft2 |
2D FFT | from scipy.fft import fft2x = np.ones((2, 2))y = fft2(x)print(y) # [[4.+0.j 0.+0.j] [0.+0.j 0.+0.j]] |
| Function/Class | Description | Example |
|---|---|---|
csr_matrix |
Compressed Sparse Row matrix | from scipy.sparse import csr_matrixdata = np.array([1, 2, 3])row = np.array([0, 0, 1])col = np.array([0, 2, 1])mat = csr_matrix((data, (row, col)), shape=(2, 3))print(mat.toarray()) # [[1 0 2] [0 3 0]] |
linalg.spsolve |
Solve sparse system | from scipy.sparse import csr_matrixfrom scipy.sparse.linalg import spsolveA = csr_matrix([[3, 2], [1, 1]])b = np.array([5, 2])x = spsolve(A, b)print(x) # [1. 1.] |
| Function | Description | Example |
|---|---|---|
distance.pdist |
Pairwise distances | from scipy.spatial import distancex = np.array([[0, 0], [0, 1], [1, 0]])d = distance.pdist(x, 'euclidean')print(d) # [1. 1. 1.41421356] |
KDTree |
KD-Tree for quick nearest-neighbor lookup | from scipy.spatial import KDTreex = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])tree = KDTree(x)dist, idx = tree.query(np.array([0.5, 0.5]), k=2)print(idx) # [0 2] |
Voronoi |
Voronoi diagrams | from scipy.spatial import Voronoipoints = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])vor = Voronoi(points)print(vor.vertices) # [[0.5 0.5]] |
| Function | Description | Example |
|---|---|---|
gaussian_filter |
Gaussian smoothing | from scipy.ndimage import gaussian_filterx = np.random.randn(10, 10)y = gaussian_filter(x, sigma=1) |
rotate |
Rotate array | from scipy.ndimage import rotatex = np.eye(4)y = rotate(x, 45) |
label |
Label features in image | from scipy.ndimage import labelx = np.array([[0, 1, 1], [0, 0, 1], [1, 1, 0]])labeled, num_features = label(x)print(num_features) # 2 |
| Constant | Value | Example |
|---|---|---|
pi |
3.141592653589793 | from scipy import constantsprint(constants.pi) # 3.141592653589793 |
c |
Speed of light (m/s) | print(constants.c) # 299792458.0 |
G |
Gravitational constant | print(constants.G) # 6.67430e-11 |
h |
Planck constant | print(constants.h) # 6.62607015e-34 |
# Global optimization
from scipy.optimize import differential_evolution
# Define objective function
def objective(x):
return x[0]**2 + x[1]**2
# Define bounds
bounds = [(-5, 5), (-5, 5)]
# Solve the minimization problem
result = differential_evolution(objective, bounds)
print(result.x) # Should be close to [0, 0]# Solving a system of ODEs
from scipy.integrate import solve_ivp
import numpy as np
import matplotlib.pyplot as plt
# Define the ODE system (Lotka-Volterra predator-prey model)
def lotka_volterra(t, z, a, b, c, d):
x, y = z
dx_dt = a*x - b*x*y
dy_dt = -c*y + d*x*y
return [dx_dt, dy_dt]
# Parameters
a, b, c, d = 1.0, 0.1, 1.5, 0.075
z0 = [10, 5] # Initial population
t_span = (0, 40) # Time span
t_eval = np.linspace(0, 40, 1000) # Points to evaluate
# Solve
sol = solve_ivp(
lambda t, z: lotka_volterra(t, z, a, b, c, d),
t_span, z0, method='RK45', t_eval=t_eval
)
# Extract results
t = sol.t
x, y = sol.yimport numpy as np
from scipy.fft import fft, fftfreq
import matplotlib.pyplot as plt
# Create a signal with two sine waves
sample_rate = 1000 # Hz
duration = 1 # second
t = np.linspace(0, duration, sample_rate, endpoint=False)
freq1, freq2 = 50, 120 # Hz
signal = np.sin(2*np.pi*freq1*t) + 0.5*np.sin(2*np.pi*freq2*t)
# Add some noise
signal += 0.2 * np.random.randn(len(t))
# Compute FFT
yf = fft(signal)
xf = fftfreq(sample_rate, 1/sample_rate)[:sample_rate//2]
yplot = 2.0/sample_rate * np.abs(yf[:sample_rate//2])
# Plot the spectrum
plt.figure(figsize=(10, 6))
plt.plot(xf, yplot)
plt.grid()
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.xlim(0, 200)from scipy.sparse import csr_matrix, diags
import numpy as np
# Create a sparse matrix from diagonals
n = 1000
diagonals = [np.ones(n), -2*np.ones(n), np.ones(n)]
offsets = [-1, 0, 1]
A = diags(diagonals, offsets, shape=(n, n), format='csr')
# Create a random sparse matrix
density = 0.01 # 1% of elements are non-zero
B = np.random.random((n, n))
B[B > density] = 0
B_sparse = csr_matrix(B)
# Sparse matrix operations
C = A @ B_sparse # Matrix multiplicationfrom scipy.spatial import Delaunay, ConvexHull
import numpy as np
import matplotlib.pyplot as plt
# Generate random points
points = np.random.rand(30, 2)
# Compute Delaunay triangulation
tri = Delaunay(points)
# Compute the convex hull
hull = ConvexHull(points)
# Plot
plt.figure(figsize=(10, 5))
plt.triplot(points[:,0], points[:,1], tri.simplices)
plt.plot(points[:,0], points[:,1], 'o')
plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'r-', lw=2)-
Import Conventions: Use named imports for clarity:
from scipy import optimize, stats, signal
-
Computational Efficiency:
- Use sparse matrices for large, sparse problems
- Vectorize operations instead of loops
- Use appropriate methods for large-scale problems (e.g.,
scipy.sparse.linalgvsscipy.linalg)
-
Error Handling:
- Check return values of optimization functions like
result.success - Use
try/exceptblocks for numerical routines that might fail
- Check return values of optimization functions like
-
Integration with NumPy and Matplotlib:
- SciPy works seamlessly with NumPy arrays
- Results are easily visualized with Matplotlib
-
Version Compatibility:
- SciPy functions and behavior can change between versions
- Check documentation for your specific version