data-science
📋 Alle kopieren SciPy Commands
📄 Generieren SciPy PDF Guide _
_
SciPy Cheatsheet
• Installation
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
oder Grundlegende Befehle
Import and Setup
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
Optimierungsgrundlagen
Command
Description
INLINE_CODE_29
Find minimum of a scalar function
INLINE_CODE_30
Minimize using BFGS algorithm
INLINE_CODE_31
Find root of a function
INLINE_CODE_32
Fit curve to data points
INLINE_CODE_33
Solve system of nonlinear equations
INLINE_CODE_34
Minimize single-variable function
INLINE_CODE_35
Linear programming optimization
_
### 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
_
### Statistiken
Command
Description
INLINE_CODE_47
Generate normal random variables
INLINE_CODE_48
One-sample t-test
INLINE_CODE_49
Independent two-sample t-test
INLINE_CODE_50
Pearson correlation coefficient
INLINE_CODE_51
Normal probability density function
INLINE_CODE_52
Cumulative distribution function
INLINE_CODE_53
Kolmogorov-Smirnov test
INLINE_CODE_54
Chi-square test of independence
Linear Algebra
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
_
/ Fortgeschrittene Nutzung
Erweiterte Optimierung
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
_
### Erweiterte Statistik
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
_
### Advanced Linear Algebra
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
Advanced Signal Processing
Command
Description
INLINE_CODE_112
Short-time Fourier transform
INLINE_CODE_113
Inverse STFT
INLINE_CODE_114
Power spectral density (Welch method)
INLINE_CODE_115
Coherence between signals
INLINE_CODE_116
Hilbert transform
INLINE_CODE_117
Savitzky-Golay filter
INLINE_CODE_118
Remove linear trend from data
INLINE_CODE_119
Wiener filter for noise reduction
Advanced Sparse Operations
Command
Description
INLINE_CODE_120
Solve sparse linear system
INLINE_CODE_121
Find k eigenvalues of sparse matrix
INLINE_CODE_122
Sparse SVD for k singular values
INLINE_CODE_123
Conjugate gradient solver
INLINE_CODE_124
GMRES iterative solver
INLINE_CODE_125
Kronecker product of sparse matrices
Spatial Operationen
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
Bildbearbeitung
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
_
Konfiguration
NumPy/SciPy Konfiguration
# 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 )
Optimierung Konfiguration
# 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 )
Integration Tolerances
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
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 )
Häufige Anwendungsfälle
Use Case 1: Curve Fitting und Modellauswahl
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: Signalfilterung und -analyse
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: Statistische Hypothesenprüfung
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: Optimierung mit Einschränkungen
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" )
oder Best Practices
**Choose the Right Method*: Wählen Sie Optimierungsalgorithmen basierend auf Problemeigenschaften (gradient-basiert für glatte Funktionen, globale Optimierer für multimodale Probleme)
Vectorize Operations : Verwenden Sie NumPy-Array-Operationen anstelle von Schleifen für bessere Leistung; SciPy-Funktionen werden für Array-Eingänge optimiert
Handle Numerical Stabilität**: Verwenden Sie geeignete Toleranzen (rtol, atol) zur Integration und Optimierung; beachten Sie die Zustandszahlen in linearen Algebra-Betrieben
**Leverage Sparse Matrices*: Für große Matrizen mit vielen Nullen verwenden Sie sparse Matrixformate (csr_matrix, csc_matrix) um Speicher zu speichern und die Rechengeschwindigkeit zu verbessern
**Provide Good Initial Guesses*: Optimierung und Root-Susting-Algorithmen konvergieren sich schneller mit vernünftigen Startpunkten; nutzen Sie Domain-Wissen, wenn möglich
** Angemessene statistische Tests*: Annahmen (Normalität, gleiche Varianz) vor Anwendung parametrischer Tests überprüfen; verwenden Sie nichtparametrische Alternativen, wenn Annahmen verletzt werden
**Set Random Seeds*: Stellen Sie die Reproduzierbarkeit in stochastischen Algorithmen sicher, indem Sie zufällige Samen mit np.random.seed() oder mit __INLINE_CODE_146_ Objekten einstellen
Profile Performance : Verwenden Sie %timeit in Jupyter oder cProfile, um Engpässe zu identifizieren; betrachten Sie Numba oder Cython für kritische Abschnitte, wenn SciPy-Funktionen unzureichend sind
**Konvergenz prüfen*: Prüfen Sie immer Optimierungsergebnisse (result.success, result.message) und Integrationsfehler vor vertrauensvollen Ausgängen
**Document Units and Scales*: Deutlich dokumentieren physikalische Einheiten und Datenwaagen; normalisieren Sie die Daten, wenn dies zur Verbesserung der numerischen Stabilität erforderlich ist
oder