💾 Archived View for gem.sdf.org › s.kaplan › cheatsheets › libraries-and-frameworks › scipy.md captured on 2024-08-18 at 18:25:02.
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
# SciPy Cheatsheet SciPy is a popular open-source Python library for scientific computing. It provides a wide range of tools for working with scientific data, including optimization, integration, interpolation, signal processing, and more. This cheatsheet provides a quick reference for some of SciPy's unique features, including code blocks for optimization, integration, interpolation, signal processing, and more. Additionally, it includes a list of resources for further learning. ## Optimization
import numpy as np
from scipy.optimize import minimize
def rosen(x):
return sum(100.0 * (x[1:] - x[:-1] ** 2.0) ** 2.0 + (1 - x[:-1]) ** 2.0)
res = minimize(rosen, np.zeros(10), method='nelder-mead', options={'xtol': 1e-8, 'disp': True})
## Integration
from scipy.integrate import quad
def f(x):
return np.exp(-x ** 2)
quad(f, 0, np.inf)
## Interpolation
from scipy.interpolate import interp1d
x = np.linspace(0, 10, num=11, endpoint=True)
y = np.cos(-x ** 2 / 9.0)
f = interp1d(x, y, kind='cubic')
xnew = np.linspace(0, 10, num=41, endpoint=True)
ynew = f(xnew)
## Signal Processing
from scipy import signal
t = np.linspace(0, 1, 1000, endpoint=False)
x = signal.square(2 * np.pi * 5 * t)
b, a = signal.butter(4, 0.1, 'lowpass')
y = signal.filtfilt(b, a, x)
## Other Useful Features
from scipy import stats
x = stats.norm.rvs(size=1000)
stats.norm.fit(x)
x = np.random.normal(size=100)
y = np.random.normal(size=100)
stats.pearsonr(x, y)
## Resources - [SciPy documentation](https://docs.scipy.org/doc/) - [SciPy tutorial](https://docs.scipy.org/doc/scipy/tutorial/index.html) - [Python Data Science Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/index.html)