SciPy Cheatsheet¶
Instalación¶
| Platform | Command |
|---|---|
| Ubuntu/Debian | INLINE_CODE_10 or INLINE_CODE_11 |
| macOS | INLINE_CODE_12 or INLINE_CODE_13 |
| Windows | INLINE_CODE_14 or INLINE_CODE_15 (recommended) |
| Conda (All platforms) | INLINE_CODE_16 or INLINE_CODE_17 |
| Virtual Environment | INLINE_CODE_18 |
| Specific Version | INLINE_CODE_19 |
| With Dependencies | INLINE_CODE_20 |
| Verify Installation | INLINE_CODE_21 |
Comandos básicos¶
Importación y configuración¶
| Command | Description |
|---|---|
| INLINE_CODE_22 | Import the SciPy library |
| INLINE_CODE_23 | Import NumPy (required for SciPy) |
| INLINE_CODE_24 | Import optimization module |
| INLINE_CODE_25 | Import statistics module |
| INLINE_CODE_26 | Import linear algebra module |
| INLINE_CODE_27 | Check SciPy version |
| INLINE_CODE_28 | Display SciPy configuration |
Optimization Basics¶
__TABLE_160_
Integration¶
| Command | Description |
|---|---|
| INLINE_CODE_36 | Integrate function from a to b |
| INLINE_CODE_37 | Double integration |
| INLINE_CODE_38 | Solve ordinary differential equations |
| INLINE_CODE_39 | Modern ODE solver with events |
| INLINE_CODE_40 | Trapezoidal rule integration |
| INLINE_CODE_41 | Simpson's rule integration |
Interpolation¶
| Command | Description |
|---|---|
| INLINE_CODE_42 | 1D interpolation function |
| INLINE_CODE_43 | Cubic spline interpolation |
| INLINE_CODE_44 | Interpolate scattered data |
| INLINE_CODE_45 | Univariate spline approximation |
| INLINE_CODE_46 | 2D interpolation function |
Statistics¶
__TABLE_163_
Algebra lineal¶
| Command | Description |
|---|---|
| INLINE_CODE_55 | Solve linear system Ax = b |
| INLINE_CODE_56 | Calculate matrix inverse |
| INLINE_CODE_57 | Calculate determinant |
| INLINE_CODE_58 | Compute eigenvalues and eigenvectors |
| INLINE_CODE_59 | Singular value decomposition |
| INLINE_CODE_60 | Least-squares solution |
| INLINE_CODE_61 | Matrix or vector norm |
| INLINE_CODE_62 | QR decomposition |
Signal Processing¶
| Command | Description |
|---|---|
| INLINE_CODE_63 | Butterworth filter design |
| INLINE_CODE_64 | Zero-phase digital filtering |
| INLINE_CODE_65 | Find peaks in signal |
| INLINE_CODE_66 | Cross-correlation of signals |
| INLINE_CODE_67 | Convolution of signals |
| INLINE_CODE_68 | Compute spectrogram |
| INLINE_CODE_69 | Resample signal to num samples |
Sparse Matrices¶
| Command | Description |
|---|---|
| INLINE_CODE_70 | Create compressed sparse row matrix |
| INLINE_CODE_71 | Create compressed sparse column matrix |
| INLINE_CODE_72 | Sparse identity matrix |
| INLINE_CODE_73 | Construct sparse diagonal matrix |
| INLINE_CODE_74 | Convert sparse to dense array |
| INLINE_CODE_75 | Convert sparse to dense matrix |
Advanced Usage¶
Optimización avanzada¶
| Command | Description |
|---|---|
| INLINE_CODE_76 | Constrained optimization with bounds |
| INLINE_CODE_77 | Global optimization using genetic algorithm |
| INLINE_CODE_78 | Global optimization with basin-hopping |
| INLINE_CODE_79 | Nonlinear least squares with bounds |
| INLINE_CODE_80 | Optimization with analytical gradient |
| INLINE_CODE_81 | Simplicial homology global optimization |
| INLINE_CODE_82 | Dual annealing global optimization |
Advanced Integration¶
| Command | Description |
|---|---|
| INLINE_CODE_83 | ODE solver with event detection |
| INLINE_CODE_84 | ODE with continuous solution |
| INLINE_CODE_85 | N-dimensional integration |
| INLINE_CODE_86 | ODE with Jacobian matrix |
| INLINE_CODE_87 | Boundary value problem solver |
| INLINE_CODE_88 | Integration with weight functions |
Advanced Interpolation¶
| Command | Description |
|---|---|
| INLINE_CODE_89 | Radial basis function interpolation |
| INLINE_CODE_90 | B-spline basis functions |
| INLINE_CODE_91 | Make interpolating B-spline |
| INLINE_CODE_92 | Akima interpolation (smooth curves) |
| INLINE_CODE_93 | PCHIP 1-D monotonic cubic interpolation |
| INLINE_CODE_94 | Interpolation on regular grid |
Advanced Statistics¶
| Command | Description |
|---|---|
| INLINE_CODE_95 | Kernel density estimation |
| INLINE_CODE_96 | Multivariate normal samples |
| INLINE_CODE_97 | Bootstrap confidence intervals |
| INLINE_CODE_98 | Anderson-Darling test |
| INLINE_CODE_99 | Mann-Whitney U test |
| INLINE_CODE_100 | Kruskal-Wallis H-test |
| INLINE_CODE_101 | Wilcoxon signed-rank test |
| INLINE_CODE_102 | Spearman rank correlation |
| INLINE_CODE_103 | Linear regression analysis |
Algebra lineal avanzada¶
| Command | Description |
|---|---|
| INLINE_CODE_104 | Cholesky decomposition |
| INLINE_CODE_105 | LU decomposition |
| INLINE_CODE_106 | Schur decomposition |
| INLINE_CODE_107 | Matrix exponential |
| INLINE_CODE_108 | Matrix logarithm |
| INLINE_CODE_109 | Matrix square root |
| INLINE_CODE_110 | Solve triangular system |
| INLINE_CODE_111 | Moore-Penrose pseudoinverse |
Procesamiento avanzado de señales¶
__TABLE_172_
Advanced Sparse Operations¶
__TABLE_173_
Operaciones espaciales¶
| Command | Description |
|---|---|
| INLINE_CODE_126 | Pairwise distances between observations |
| INLINE_CODE_127 | Distance between two collections |
| INLINE_CODE_128 | k-d tree for fast nearest neighbor |
| INLINE_CODE_129 | Convex hull of points |
| INLINE_CODE_130 | Delaunay triangulation |
| INLINE_CODE_131 | Voronoi diagram |
| INLINE_CODE_132 | Distance matrix computation |
Image Processing¶
| Command | Description |
|---|---|
| INLINE_CODE_133 | Gaussian smoothing filter |
| INLINE_CODE_134 | Median filter for noise reduction |
| INLINE_CODE_135 | Rotate image by angle |
| INLINE_CODE_136 | Zoom image by factor |
| INLINE_CODE_137 | Binary morphological erosion |
| INLINE_CODE_138 | Binary morphological dilation |
| INLINE_CODE_139 | Label connected components |
| INLINE_CODE_140 | Sobel edge detection |
Configuración¶
NumPy/SciPy Configuration¶
# Display build and configuration information
import scipy
scipy.show_config()
# Set NumPy print options (affects SciPy output)
import numpy as np
np.set_printoptions(precision=4, suppress=True, linewidth=100)
# Configure warning filters
import warnings
warnings.filterwarnings('ignore', category=RuntimeWarning)
Optimización Configuración¶
# Configure optimization options
from scipy.optimize import minimize
options = {
'maxiter': 1000, # Maximum iterations
'disp': True, # Display convergence messages
'ftol': 1e-8, # Function tolerance
'gtol': 1e-8 # Gradient tolerance
}
result = minimize(func, x0, method='BFGS', options=options)
Integración Tolerancias¶
from scipy.integrate import quad, solve_ivp
# Configure integration accuracy
result, error = quad(func, a, b,
epsabs=1e-10, # Absolute error tolerance
epsrel=1e-10, # Relative error tolerance
limit=100) # Subdivision limit
# Configure ODE solver
sol = solve_ivp(func, t_span, y0,
method='RK45',
rtol=1e-6, # Relative tolerance
atol=1e-9, # Absolute tolerance
max_step=0.1) # Maximum step size
Selección de Formato de Matrix¶
from scipy import sparse
# Choose format based on use case
# CSR: efficient row slicing, matrix-vector products
A_csr = sparse.csr_matrix(data)
# CSC: efficient column slicing, matrix-vector products
A_csc = sparse.csc_matrix(data)
# COO: efficient construction, conversion
A_coo = sparse.coo_matrix(data)
# LIL: efficient incremental construction
A_lil = sparse.lil_matrix((1000, 1000))
Random Number Generation¶
from scipy import stats
import numpy as np
# Set random seed for reproducibility
np.random.seed(42)
# Use RandomState for thread-safe operations
rng = np.random.RandomState(42)
data = stats.norm.rvs(loc=0, scale=1, size=1000, random_state=rng)
Common Use Cases¶
Use Case 1: Curve Fitting and Model Selection¶
import numpy as np
from scipy.optimize import curve_fit
from scipy import stats
import matplotlib.pyplot as plt
# Generate noisy data
x = np.linspace(0, 10, 100)
y_true = 2.5 * np.exp(-0.5 * x) + 1.0
y_noisy = y_true + 0.2 * np.random.normal(size=len(x))
# Define model
def exponential_model(x, a, b, c):
return a * np.exp(-b * x) + c
# Fit curve
params, covariance = curve_fit(exponential_model, x, y_noisy)
y_fit = exponential_model(x, *params)
# Calculate R-squared
residuals = y_noisy - y_fit
ss_res = np.sum(residuals**2)
ss_tot = np.sum((y_noisy - np.mean(y_noisy))**2)
r_squared = 1 - (ss_res / ss_tot)
print(f"Parameters: a={params[0]:.3f}, b={params[1]:.3f}, c={params[2]:.3f}")
print(f"R-squared: {r_squared:.4f}")
Use Case 2: Signal Filtering and Analysis¶
import numpy as np
from scipy import signal
from scipy.fft import fft, fftfreq
# Create noisy signal
fs = 1000 # Sampling frequency
t = np.linspace(0, 1, fs)
clean_signal = np.sin(2 * np.pi * 50 * t) + np.sin(2 * np.pi * 120 * t)
noisy_signal = clean_signal + 0.5 * np.random.normal(size=len(t))
# Design and apply Butterworth filter
sos = signal.butter(10, 100, btype='low', fs=fs, output='sos')
filtered_signal = signal.sosfilt(sos, noisy_signal)
# Find peaks
peaks, properties = signal.find_peaks(filtered_signal,
height=0.5,
distance=20)
# Compute power spectral density
f, Pxx = signal.welch(filtered_signal, fs, nperseg=256)
print(f"Found {len(peaks)} peaks")
print(f"Dominant frequency: {f[np.argmax(Pxx)]:.2f} Hz")
Use Case 3: Statistical Hypothesis Testing¶
import numpy as np
from scipy import stats
# Generate two sample datasets
np.random.seed(42)
group1 = stats.norm.rvs(loc=100, scale=15, size=50)
group2 = stats.norm.rvs(loc=105, scale=15, size=50)
# Test for normality
_, p_norm1 = stats.shapiro(group1)
_, p_norm2 = stats.shapiro(group2)
# Test for equal variances
_, p_var = stats.levene(group1, group2)
# Perform appropriate t-test
if p_var > 0.05:
# Equal variances
t_stat, p_value = stats.ttest_ind(group1, group2)
test_type = "Independent t-test (equal variances)"
else:
# Unequal variances (Welch's t-test)
t_stat, p_value = stats.ttest_ind(group1, group2, equal_var=False)
test_type = "Welch's t-test (unequal variances)"
# Calculate effect size (Cohen's d)
pooled_std = np.sqrt((np.std(group1)**2 + np.std(group2)**2) / 2)
cohens_d = (np.mean(group1) - np.mean(group2)) / pooled_std
print(f"Test: {test_type}")
print(f"t-statistic: {t_stat:.4f}, p-value: {p_value:.4f}")
print(f"Cohen's d: {cohens_d:.4f}")
Use Case 4: Optimization with Constraints¶
import numpy as np
from scipy.optimize import minimize
# Portfolio optimization: maximize return, minimize risk
def portfolio_objective(weights, returns, cov_matrix, risk_aversion=1.0):
portfolio_return = np.sum(returns * weights)
portfolio_risk = np.sqrt(np.dot(weights.T, np.dot(cov_matrix, weights)))
return -(portfolio_return - risk_aversion * portfolio_risk)
# Sample data
n_assets = 4
returns = np.array([0.10, 0.12, 0.15, 0.08])
cov_matrix = np.array([[0.005, -0.001, 0.001, 0.000],
[-0.001, 0.008, 0.002, 0.001],
[0.001, 0.002, 0.012, 0.003],
[0.000, 0.001, 0.003, 0.004]])
# Constraints: weights sum to 1
constraints = {'type': 'eq', 'fun': lambda x: np.sum(x) - 1}
# Bounds: each weight between 0 and 1
bounds = tuple((0, 1) for _ in range(n_assets))
# Initial guess
x0 = np.array([1/n_assets] * n_assets)
# Optimize
result = minimize(portfolio_objective, x0,
args=(returns, cov_matrix),
method='SLSQP',
bounds=bounds,
constraints=constraints)
optimal_weights = result.x
print("Optimal portfolio weights:")
for i, weight in enumerate(optimal_weights):
print(f"Asset {i+1}: {weight*100:.2f}%")
Use Case 5: Image Processing Pipeline¶
import numpy as np
from scipy import ndimage
from scipy import signal
# Load or create image (grayscale)
image = np.random.rand(256, 256) * 255
# Step 1: Denoise with Gaussian filter
denoised = ndimage.gaussian_filter(image, sigma=2)
# Step 2: Edge detection
edges_x = ndimage.sobel(denoised, axis=0)
edges_y = ndimage.sobel(denoised, axis=1)
edges = np.hypot(edges_x, edges_y)
# Step 3: Threshold to create binary image
threshold = np.mean(edges) + np.std(edges)
binary = edges > threshold
# Step 4: Morphological operations
struct = ndimage.generate_binary_structure(2, 2)
cleaned = ndimage.binary_opening(binary, structure=struct)
cleaned = ndimage.binary_closing(cleaned, structure=struct)
# Step 5: Label connected components
labeled, num_features = ndimage.label(cleaned)
# Step 6: Calculate properties
sizes = ndimage.sum(cleaned, labeled, range(num_features + 1))
centers = ndimage.center_of_mass(cleaned, labeled, range(1, num_features + 1))
print(f"Found {num_features} objects")
print(f"Average object size: {np.mean(sizes[1:]):.2f} pixels")
Buenas prácticas¶
-
Elige el método adecuado: Seleccione algoritmos de optimización basados en características de problemas (basado en grado para funciones suaves, optimizadores globales para problemas multimodales)
-
Vectorize Operations: Use NumPy array operations instead of loops for better performance; Las funciones SciPy están optimizadas para los insumos de array
-
¿Qué? Estabilidad**: Usar tolerancias apropiadas (
rtol,atol) para la integración y optimización; ser consciente de los números de condición en las operaciones de álgebra lineal -
**Mátricas de fuga* Para matrices grandes con muchos ceros, utilice formatos de matriz escasos (
csr_matrix,csc_matrix) para guardar memoria y mejorar la velocidad de cálculo -
Probar buenas ideas iniciales: Los algoritmos de optimización y determinación de raíces convergen más rápido con puntos de partida razonables; utilizar el conocimiento de dominio cuando sea posible
-
Utilizar los exámenes estadísticos apropiados: Verificar los supuestos (normalidad, igual diferencia) antes de aplicar pruebas paramétricas; utilizar alternativas no paramétricas cuando se violan las hipótesis
-
Set Random Seeds: Asegurar la reproducibilidad en algoritmos estocásticos estableciendo semillas aleatorias con
np.random.seed()o utilizando objetos __INLINE_CODE_146_ -
Rendimiento del archivo: Uso
%timeit_ en Jupyter ocProfile_ para identificar los cuellos de botella; considere Numba o Cython para secciones críticas si las funciones de SciPy son insuficientes -
Mira Convergencia. Examinar siempre los resultados de optimización (INLINE_CODE_149__, __INLINE_CODE_150_) y errores de integración antes de confiar en las salidas
-
** Unidades de documentos y escalas**: documentar claramente las unidades físicas y las escalas de datos; normalizar los datos cuando proceda para mejorar la estabilidad numérica