Python — SciPy
Install & relationship to NumPy
pip install scipy
SciPy builds ON NumPy: same arrays, plus a library of tested scientific algorithms. Rule of thumb — NumPy stores the numbers; SciPy computes the hard stuff.
import numpy as np
import scipy
Constants & special values
from scipy import constants
constants.speed_of_light # 299792458.0
constants.g # 9.80665
constants.pi # (np.pi too)
Statistics — the most-used module
from scipy import stats
data = [22, 25, 21, 28, 90] # 90 is an outlier
stats.describe(data) # n, minmax, mean, variance, skew…
stats.mode(data)
# z-scores: how many standard deviations from the mean?
z = stats.zscore(data) # 90 → ~2.1 → outlier flag
# distributions:
stats.norm.pdf(0) # normal curve height at 0
stats.norm.cdf(1.96) # area left of 1.96 ≈ 0.975
# statistical tests:
group_a = [20, 22, 19, 24]
group_b = [30, 28, 32, 27]
t, p = stats.ttest_ind(group_a, group_b)
p < 0.05 # is the difference statistically meaningful?
Optimization — finding minimums
from scipy.optimize import minimize
def cost(x):
return (x - 3) ** 2 + 5 # parabola with minimum at x=3
result = minimize(cost, x0=0) # start searching from 0
result.x # array([2.999…]) — found it!
result.fun # minimum value ≈ 5
Also minimize_scalar, linprog (linear programming), curve_fit (fit equations to data).
Interpolation — filling gaps
from scipy.interpolate import interp1d
x = [0, 10, 20]
y = [0, 100, 200]
f = interp1d(x, y) # linear in-betweens
f(5) # 50.0 — estimated value between points
f_smooth = interp1d(x, y, kind="cubic") # smooth curves through points
Signal & image quickies
from scipy import signal, ndimage
# smooth noisy data with a moving filter:
clean = signal.savgol_filter(noisy, window_length=11, polyorder=2)
# rotate/zoom/filter images as arrays:
rotated = ndimage.rotate(img_array, angle=45)
blurred = ndimage.gaussian_filter(img_array, sigma=2)
Sparse matrices (awareness)
Huge mostly-zero grids (graphs, recommender data) waste memory densely:
from scipy.sparse import csr_matrix
sparse = csr_matrix(big_and_mostly_zero_array) # stores only nonzeros
When to reach for SciPy vs hand-rolling
| Need | Module |
|---|---|
| Means/spreads/tests | scipy.stats |
| Find optimum of function | scipy.optimize |
| Estimate between samples | scipy.interpolate |
| Clean noisy signals | scipy.signal |
| Array math only | stay in NumPy |
Gotcha: SciPy functions return NumPy arrays/objects with rich attributes (
result.x,.pvalue) — print the result object first to see what you got.
Mini Practice
- describe() + zscore on a list with one obvious outlier.
- t-test two grade lists; interpret p-value.
- minimize(x-5)²+sin(x); verify against graph.
- Linear-interpolate temperature gaps in hourly readings.
- Savgol-smooth random noise; plot raw vs clean.
Python track complete — every syllabus topic covered. 🐍✅
Related Topics
Frequently Asked Questions about SciPy
What is SciPy in Python?
SciPy is a fundamental concept in Python. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn SciPy?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn SciPy.
Why is SciPy important in Python?
SciPy is essential for Python development. Understanding this concept will help you write better code and solve real-world problems more effectively.