SciPy Getting Started
To use SciPy on your own computer you install it once, then import the parts you need. On this site everything is already installed, so you can skip straight to the examples.
Installing SciPy
SciPy is published on PyPI and conda-forge. NumPy is installed automatically as a dependency.
pip install scipy # with pip
conda install scipy # with conda / mambaMost people also install Matplotlib for plotting, since visualising results is half the work in scientific computing:
pip install scipy matplotlibImporting SciPy
SciPy is a collection of subpackages. Unlike NumPy, the convention is not to import the whole library under a short alias. Instead you import the subpackage (or the specific function) you need:
from scipy import constants
from scipy import stats
from scipy.optimize import minimize_scalar
print(constants.pi)
print(stats.norm.mean())
print(minimize_scalar(lambda x: x ** 2 - 4 * x).x)3.141592653589793
0.0
2.0
The common import styles are:
| Style | Example | When to use |
|---|---|---|
| Subpackage | from scipy import stats then stats.ttest_ind(...) |
Most readable; makes the origin of each function obvious |
| Single function | from scipy.optimize import curve_fit |
When you use one or two functions a lot |
| Dotted module | import scipy.linalg as la |
When you want a short alias for a subpackage |
Tip: Modern SciPy (1.9 and later) loads subpackages lazily, so
import scipyfollowed byscipy.stats.normalso works. Explicit subpackage imports are still the clearest style and are what you'll see in most code.
Checking the version
Features are added in every release, so it's useful to know which version you are running. Every SciPy installation exposes scipy.__version__:
import scipy
import numpy as np
print("SciPy:", scipy.__version__)
print("NumPy:", np.__version__)SciPy: 1.14.1
NumPy: 2.2.5
Your first real program
Let's solve a small, practical problem. A rectangular garden bed should have an area of 24 m² and we want to use as little fencing as possible. If one side is w metres, the other is 24 / w, so the perimeter is 2w + 48/w. SciPy finds the best w for us:
from scipy.optimize import minimize_scalar
def perimeter(w):
return 2 * w + 2 * (24 / w)
best = minimize_scalar(perimeter, bounds=(0.5, 20), method="bounded")
width = best.x
print(f"width = {width:.3f} m")
print(f"length = {24 / width:.3f} m")
print(f"fence = {best.fun:.3f} m")width = 4.899 m
length = 4.899 m
fence = 19.596 m
The answer is a square with sides of about 4.899 m (that is, √24), which matches the textbook result that a square has the smallest perimeter for a given area.
Getting help
Every SciPy function has a detailed docstring. In a notebook or Python shell, use help() or a question mark:
from scipy import integrate
help(integrate.quad) # full documentation
integrate.quad? # IPython / Jupyter shortcutYou can also print just the first line of a docstring to see what a function does:
from scipy import integrate, optimize, stats
for func in [integrate.quad, optimize.brentq, stats.pearsonr]:
first_line = func.__doc__.strip().splitlines()[0]
print(f"{func.__name__}: {first_line}")quad: Compute a definite integral.
brentq: Find a root of a function in a bracketing interval using Brent's method.
pearsonr: Pearson correlation coefficient and p-value for testing non-correlation.
Note: The official documentation at docs.scipy.org has a reference page for every function, plus tutorials for each subpackage. When a lesson here mentions an option we don't cover, that is the place to look.
Test yourself
Which import style is the usual convention for SciPy?
- import scipy as sp, then sp.everything
- from scipy import stats (import the subpackage you need)
- from scipy import *
- import numpy.scipy
How do you check which SciPy version is installed?
- scipy.version()
- scipy.__version__
- pip.scipy
- scipy.info