What SciPy is and how it is organised

SciPy · Essentials · 6 min

Knowing which subpackage holds which algorithms saves you from reinventing them with NumPy loops.

NumPy gives you arrays and basic maths on them; SciPy adds the numerical algorithms you build on those arrays. It is organised into subpackages, each a field of scientific computing:

  • scipy.stats: probability distributions, descriptive statistics, hypothesis tests.
  • scipy.optimize: minimisation, curve fitting, root finding, linear programming.
  • scipy.integrate: numerical integrals and ODE solvers.
  • scipy.interpolate: splines and interpolation.
  • scipy.linalg and scipy.sparse: dense and sparse linear algebra.
  • scipy.signal, scipy.fft: filtering, spectral analysis, Fourier transforms.
  • scipy.spatial: KD-trees, distances, convex hulls; scipy.special: special functions such as gamma, erf, expit.
  • scipy.ndimage, scipy.io, scipy.cluster, scipy.constants and more.

Import subpackages, not the top-level package: from scipy import stats, optimize. Then call stats.norm.cdf(...). Writing import scipy and then scipy.stats also works in current versions because subpackages load lazily, but the explicit form is the documented style and keeps names short.

Nearly every function accepts and returns NumPy arrays, and many are vectorised: pass an array of inputs and get an array of results. Results of more complex routines come back as result objects with named attributes (res.x, res.pvalue) rather than bare tuples, so read the attribute names in the docs.

Example

import numpy as np
from scipy import constants, special, stats

print(constants.c)                     # 299792458.0 (speed of light, m/s)
print(special.comb(10, 3))             # 120.0 (10 choose 3)
print(special.expit(0.0))              # 0.5 (logistic sigmoid)

x = np.array([-1.96, 0.0, 1.96])
print(stats.norm.cdf(x).round(3))      # [0.025 0.5   0.975] (vectorised)

res = stats.ttest_1samp([5.1, 4.9, 5.3, 5.2], popmean=5.0)
print(type(res).__name__, round(res.pvalue, 3))  # TtestResult 0.239

Remember

  • SciPy = numerical algorithms on top of NumPy arrays, grouped by subpackage
  • Import as from scipy import stats, optimize
  • Complex routines return result objects with named attributes

Watch out for

  • Writing a numerical method by hand (a root finder, an integrator) that SciPy already provides.

Check yourself

  1. Where would you look for a function that finds the minimum of a cost function?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.scipy.org/doc/scipy/reference/index.html