Python - SciPy

Overview

Estimated time: 25–40 minutes

SciPy builds on NumPy to provide scientific algorithms (optimize, stats, signal). Learn the landscape and common tasks.

Learning Objectives

  • Use scipy.optimize for minimization/curve fitting.
  • Apply scipy.stats for distributions and tests.

Examples

import numpy as np
from scipy.optimize import curve_fit

x = np.linspace(0, 1, 20)
y = 2.0 * x + 1.0 + np.random.normal(scale=0.1, size=x.shape)

def f(x, a, b):
    return a * x + b

popt, pcov = curve_fit(f, x, y)
print(popt)  # ~[2.0, 1.0]

Guidance & Patterns

  • Prefer NumPy for array ops; turn to SciPy for algorithms.
  • Check shapes and initial guesses for stable fits.

Best Practices

  • Pin versions for reproducible results; document seeds where randomness is used.

Exercises

  1. Fit a polynomial curve to noisy data and plot residuals.
  2. Run a hypothesis test using scipy.stats and interpret results.