Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท Getting Started

What Is SciPy?

Learn what SciPy is, how it differs from NumPy and pandas, which submodules it offers, and when to reach for it. Includes live examples you can run.

SciPy is Python's library for scientific computing. It gives you tested, fast implementations of the numerical algorithms that engineers, scientists and data analysts need again and again: solving equations, finding the minimum of a function, integrating a curve, fitting a model to measurements, filtering a noisy signal, running a statistical test, working with huge mostly-empty matrices.

You could write many of these algorithms yourself, but you would spend weeks and still be slower and less accurate than the versions in SciPy, which have been refined by numerical analysts for decades. This tutorial teaches you to pick the right tool, call it correctly and, just as important, understand when its answer can be trusted.

You should know basic Python (functions, modules) and have seen NumPy arrays. The next lesson is a short NumPy refresher, so you do not need to be an expert yet.

SciPy at a glance

Optimization Find the lowest point of a function or fit a curve to data.
Integration Add up area and solve equations that describe change.
Linear algebra Solve systems and factor matrices.
Signals and images Filter, transform and measure.
1 / 2

SciPy, NumPy and pandas: who does what

The three libraries are often used together, and each has a clear job:

Library Main idea Typical question
NumPy Fast n-dimensional arrays and element-wise math "Add these two million numbers"
pandas Labeled tables (DataFrames) with missing data, grouping, time series "Average sales per region per month"
SciPy Algorithms that operate on arrays "Find the parameters that best fit this curve"

NumPy provides the container; SciPy provides the algorithms. Almost every SciPy function accepts NumPy arrays and returns NumPy arrays or small result objects that hold arrays. pandas columns convert to arrays automatically, so the three combine naturally.

flowchart TD
    P["pandas<br/>labeled tables"] --> N["NumPy<br/>ndarray"]
    S["SciPy<br/>algorithms"] --> N
    M["Matplotlib<br/>plots"] --> N
    N --> B["BLAS and LAPACK<br/>compiled Fortran and C"]
    S --> B

NumPy arrays are the shared data format; SciPy adds algorithms on top and relies on compiled numerical libraries underneath.

What SciPy is built on

SciPy is mostly a friendly Python layer over very old and very fast compiled code. Dense linear algebra goes through BLAS (basic matrix and vector operations) and LAPACK (decompositions and solvers), both written originally in Fortran and optimized for each processor. Optimization, integration and differential-equation solvers wrap classic libraries such as QUADPACK, MINPACK and ODEPACK, plus newer code in C, C++ and Cython. Python only decides what to call; the heavy loops run in compiled code.

This explains an important habit: loops in Python are slow, calls into SciPy are fast. You get speed by handing whole arrays to a function instead of looping over elements yourself.

The submodule map

SciPy is split into submodules, each about one topic. Unlike NumPy, you import the submodule you need explicitly:

Submodule What it covers Lesson
scipy.constants Physical constants and unit conversions Constants
scipy.linalg Matrices, solvers, decompositions Linear algebra
scipy.optimize Minimizing, root finding, curve fitting, linear programming Optimization
scipy.integrate Integrals and differential equations Integration
scipy.interpolate Filling in values between data points, splines Interpolation
scipy.stats Distributions, hypothesis tests, correlation Statistics
scipy.fft and scipy.signal Fourier transforms, filters, spectra, peaks Signals
scipy.sparse and scipy.sparse.linalg Matrices that are mostly zeros Sparse
scipy.spatial Nearest neighbors, convex hulls, distances Spatial
scipy.ndimage Image filtering and measurement Images
scipy.special Gamma, Bessel, error functions and friends Special functions
scipy.cluster k-means and hierarchical clustering Clustering

Importing scipy alone gives you very little. Write from scipy import optimize or import scipy.stats as st for what you use.

Python Check the submodules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A first taste

Three tiny programs show the flavour. Each solves a different kind of problem with one call: integrate a function, minimize a function, and compare two groups statistically.

Python Integrate, minimize, test

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The integral of sine over a half period is exactly 2. The minimum of a parabola centered at 3 sits at 3 with value 1. And the two groups, whose true averages differ by 1.5, give a tiny p-value, so we would conclude they differ. You will learn what each call really does in later chapters. For now notice the pattern: import a submodule, call one function with plain arrays or functions, read the answer from an object.

When to reach for SciPy

Use SciPy when the problem is a standard numerical task with a well-known algorithm:

  • You must solve A x = b, find eigenvalues or compute a matrix decomposition.
  • You want the parameters that minimize an error, or the root of an equation.
  • You need an integral or a differential equation solved numerically.
  • You need a statistical test or distribution that pandas does not provide.
  • Your matrix is huge but nearly empty, so a dense array would not fit in memory.

Reach for something else when the task is table manipulation (pandas), when you need deep learning or automatic differentiation on a GPU (PyTorch, JAX), or when a dedicated library such as scikit-learn or statsmodels already wraps the algorithm with a higher-level API. SciPy is the layer underneath many of those libraries.

Here is one more example of why SciPy matters: sparse storage. A tridiagonal matrix with 400 rows has 160,000 entries, but only about 1,200 are non-zero.

Python Why sparse matrices exist

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The sparse version stores about 80 times fewer bytes and gives the same matrix-vector product. For a 100,000 by 100,000 problem the dense array would need 80 GB, while the sparse one stays tiny.

SciPy in your browser

The editors on this site run SciPy for real, inside your browser, using Pyodide: a build of Python and the scientific stack compiled to WebAssembly. The first Run downloads Python, NumPy and SciPy (a few seconds); later runs are fast. A few limits to keep in mind:

  • There is no network, no real file system and no threads or subprocesses.
  • Each run has a 30-second limit, so examples use small data.
  • Compiled libraries are the same code as on your computer, but tiny floating-point differences in the last digits are possible, which is why we round printed values.

On your own machine you install everything with pip install numpy scipy matplotlib, and the code in this tutorial runs unchanged.

Note: SciPy follows the same import convention in every example here: import numpy as np, then from scipy import <submodule>. Fixed random seeds make output repeatable.

Installing SciPy on your own computer

SciPy needs Python and NumPy. The usual route is a virtual environment and pip, which downloads a ready-made binary ("wheel") for Windows, macOS and Linux, so no compiler is needed. Check that Python is on your path first (python --version; on macOS and Linux the command may be python3).

Terminal
# 1. create and activate an isolated environment (recommended)
python -m venv .venv
.venv\Scripts\activate           # Windows (PowerShell or cmd)
source .venv/bin/activate        # macOS and Linux

# 2. keep pip itself current, then install the stack
python -m pip install --upgrade pip
python -m pip install numpy scipy matplotlib

Your options, by platform:

Platform Way to install Notes
Windows py -m pip install scipy or conda the py launcher chooses the Python for you
macOS python3 -m pip install scipy (works on Apple Silicon and Intel) Homebrew Python needs a virtual environment (brew install python)
Linux python3 -m pip install scipy in a virtual environment a system package is also available: sudo apt install python3-scipy (Debian, Ubuntu) or sudo dnf install python3-scipy (Fedora), but those versions lag behind
Anaconda or Miniconda conda install scipy conda also installs the compiled math libraries; best if you use Anaconda already
PyCharm Settings, then Python Interpreter, then the + button, search "scipy", Install Package the same as pip install inside the project's environment; Terminal in PyCharm works too
VS Code or Jupyter pip install scipy in the environment you selected as kernel restart the kernel after installing

Avoid sudo pip install on Linux and macOS: it modifies the system Python and can break the operating system's own tools. Building SciPy from source is only for developers; it needs a C, C++ and Fortran compiler and takes a long time, so choose it only if no wheel exists for your platform.

Check that it worked

Two lines tell you the version and whether the compiled parts work. The browser runner below shows the same checks:

Python Verify an installation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If import scipy fails with ModuleNotFoundError, the package was installed into a different Python than the one that runs your code: run python -m pip install scipy with the same python command you use to run the script, and check python -c "import sys; print(sys.executable)". SciPy 1.18 requires a recent Python and NumPy (see the release notes for the exact minimums), so an old interpreter may install an older SciPy than this tutorial uses.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main relationship between NumPy and SciPy?

    SciPy functions take and return NumPy arrays. NumPy supplies the data container and basic math; SciPy adds higher-level algorithms such as solvers, optimizers and statistical tests.
  2. 2. Why are SciPy calls much faster than writing the same algorithm with Python loops?

    The Python layer only prepares and dispatches the call. The loops themselves run in optimized compiled code written in Fortran, C or C++.
  3. 3. Which line correctly gets the statistics tools?

    The submodule is called stats, and the clearest way to get it is from scipy import stats (or import scipy.stats). The other forms use a wrong module name or invalid syntax.
  4. 4. When is a sparse matrix a good choice?

    Sparse formats store only the non-zero values and their positions, which saves memory and time when the matrix is mostly zeros.

Summary

  • SciPy is a library of numerical algorithms built on NumPy arrays and on compiled BLAS, LAPACK and similar code.
  • NumPy is the array container, pandas handles labeled tables, and SciPy supplies the algorithms.
  • Functionality lives in submodules such as linalg, optimize, integrate, stats, signal and sparse, which you import explicitly.
  • Hand whole arrays to SciPy instead of looping in Python.
  • The examples on this site run in your browser through Pyodide, with small data and a 30-second limit.

Next: NumPy Refresher for SciPy.