Scipy
SciPy (Scientific Python) is a collection of mathematical algorithms and convenience functions built on the NumPy extension of Python. It adds significant power to the interactive Python session by providing user-friendly and efficient numerical routines, such as routines for numerical integration, interpolation, optimization, linear algebra, and statistics.
SciPy is open-source software, which means that it is free to use and modify. It is also well-supported, with a large community of users and developers contributing to its growth and improvement.
Some of the key areas where SciPy is used include:
Numerical Integration: SciPy can help you solve problems involving integrals, differential equations, and initial value problems.
Interpolation: SciPy can help you estimate values between data points, which is useful for creating smooth curves or surfaces from discrete data.
Optimization: SciPy can help you find the best solution to a problem, such as minimizing a function or finding the roots of an equation.
Linear Algebra: SciPy can help you solve systems of linear equations, compute eigenvalues and eigenvectors, and perform other matrix operations.
Statistics: SciPy provides a wide range of statistical functions, such as probability distributions, hypothesis tests, and correlation coefficients.
To use SciPy, you need to have Python installed on your computer. You can then install SciPy using a package manager like pip or conda. Once you have SciPy installed, you can import it into your Python code using the following line:
import scipy
After importing SciPy, you can access its various functions and modules by specifying the appropriate submodule, such as scipy.integrate
for numerical integration or scipy.stats
for statistics.
SciPy is a powerful tool for scientific computing, and it is widely used in fields such as physics, engineering, biology, and finance. By learning how to use SciPy, you can gain valuable skills for working with data and solving complex problems.
Some Important Functions of SciPy
Importing scipy …
import scipy as sp
from scipy import stats, optimize, interpolate, integrate, signal, linalg
import numpy as np
T-Test
t_statistic, p_value = stats.ttest_ind(np.random.normal(0, 1, 100), np.random.normal(0, 1, 100))
Chi-Square Test
chi2_statistic, p_value = stats.chisquare([16, 18, 16, 14, 12, 12])
ANOVA
f_statistic, p_value = stats.f_oneway(np.random.normal(0, 1, 100), np.random.normal(0, 1, 100), np.random.normal(0, 1, 100))
Pearson Correlation
r, p_value = stats.pearsonr(np.random.normal(0, 1, 100), np.random.normal(0, 1, 100))
Spearman Correlation
rho, p_value = stats.spearmanr(np.random.normal(0, 1, 100), np.random.normal(0, 1, 100))
Minimize Function
def f(x): return x**2 + 10*np.sin(x) result = optimize.minimize(f, x0=0)
Root Finding
def f(x): return x**3 - 1 root = optimize.root(f, x0=0.5)
Curve Fitting
def f(x, a, b): return a * np.exp(-b * x) xdata = np.linspace(0, 4, 50) y = f(xdata, 2.5, 1.3) + 0.2 * np.random.normal(size=xdata.size) popt, _ = optimize.curve_fit(f, xdata, y)
Interpolation
x = np.arange(0, 10) y = np.exp(-x/3.0) f = interpolate.interp1d(x, y)
Integration
def f(x): return np.exp(-x**2) result, _ = integrate.quad(f, -np.inf, np.inf)
Ordinary Differential Equation Solver
def model(y, t, k): dydt = -k * y solution = integrate.odeint(model, y0=1, t=np.linspace(0, 10), args=(0.2,))
Fast Fourier Transform
t = np.linspace(0, 1, 1000) y = np.sin(2 * np.pi * 10 * t) + 0.5 * np.sin(2 * np.pi * 20 * t) yf = sp.fft.fft(y)
Signal Filtering
t = np.linspace(0, 1, 1000, False) sig = np.sin(2*np.pi*10*t) + np.sin(2*np.pi*20*t) sig_noise = sig + 2.5*np.random.randn(len(t)) sig_filt = signal.medfilt(sig_noise, 21)
Linear Algebra - Eigenvalues
A = np.array([[1, 2], [3, 4]]) eigenvalues, eigenvectors = linalg.eig(A)
Linear Algebra - Solve Linear System
A = np.array([[1, 2], [3, 4]]) b = np.array([5, 6]) x = linalg.solve(A, b)
Sparse Matrix
from scipy import sparse A = sparse.csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
Distance Computation
from scipy.spatial import distance dist = distance.euclidean([1, 0, 0], [0, 1, 0])
Clustering
from scipy.cluster.hierarchy import dendrogram, linkage X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) Z = linkage(X, 'ward')
Optimization - Linear Programming
from scipy.optimize import linprog c = [-1, 4] A = [[-3, 1], [1, 2]] b = [6, 4] res = linprog(c, A_ub=A, b_ub=b)
Convolution
from scipy import signal x = np.array([1, 2, 3]) h = np.array([0, 1, 0.5]) y = signal.convolve(x, h)
Image Processing
from scipy import ndimage im = np.zeros((256, 256)) im[64:-64, 64:-64] = 1 im = ndimage.rotate(im, 15, mode='constant')
Wavelet Transform
from scipy import signal t = np.linspace(-1, 1, 200, endpoint=False) sig = np.cos(2 * np.pi * 7 * t) + signal.gausspulse(t - 0.4, fc=2) widths = np.arange(1, 31) cwtmatr = signal.cwt(sig, signal.ricker, widths)
Special Functions
from scipy import special x = np.linspace(0, 10, 1000) y = special.jv(0, x) # Bessel function of the first kind of order 0
Numerical Differentiation
def f(x): return x**2 df = sp.misc.derivative(f, 1.0, dx=1e-6)
Probability Distributions
from scipy import stats x = np.linspace(-5, 5, 100) y = stats.norm.pdf(x, 0, 1)
Kernel Density Estimation
from scipy import stats data = np.random.normal(0, 1, 1000) kde = stats.gaussian_kde(data) x = np.linspace(-4, 4, 100) y = kde(x)
Hypothesis Testing
from scipy import stats data = np.random.normal(0, 1, 1000) statistic, p_value = stats.normaltest(data)
Sparse Eigenvalue Problem
from scipy import sparse from scipy.sparse.linalg import eigs A = sparse.rand(1000, 1000, density=0.01, format='csr') eigenvalues, eigenvectors = eigs(A, k=5)
Savitzky-Golay Filter
from scipy.signal import savgol_filter x = np.linspace(0, 2*np.pi, 100) y = np.sin(x) + np.random.random(100) * 0.2 yhat = savgol_filter(y, 51, 3)
Delaunay Triangulation
from scipy.spatial import Delaunay points = np.random.rand(30, 2) tri = Delaunay(points)