NumPy Getting Started
On this site NumPy is already available: every python playground that imports it loads NumPy automatically (the first run takes a few seconds while the package downloads). On your own computer you install it once with a package manager.
Installing NumPy on your machine
If Python and pip are installed, open a terminal and run:
pip install numpyWith the Anaconda or Miniconda distributions, NumPy is included already, or you can install it with conda install numpy. Inside a project it is a good idea to use a virtual environment so each project has its own package versions:
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install numpyImporting NumPy
Once installed, you import the package in your program. Almost everybody imports it under the short alias np, and you will see that alias in documentation, Stack Overflow answers and every lesson here:
import numpy as np
numbers = np.array([3, 1, 4, 1, 5])
print(numbers)
print(type(numbers))[3 1 4 1 5]
<class 'numpy.ndarray'>
np.array() turns a Python sequence into an ndarray. You could write import numpy and then numpy.array(...), but the alias keeps code short and familiar to other readers.
Checking the version
NumPy stores its version in np.__version__. Checking it is the first thing to do when an example from the internet behaves differently on your machine, because some functions changed between NumPy 1.x and 2.x.
import numpy as np
print("NumPy version:", np.__version__)
major = int(np.__version__.split(".")[0])
print("Running NumPy 2 or newer?", major >= 2)NumPy version: 2.2.5
Running NumPy 2 or newer? True
A first real calculation
Let's put NumPy to work. A runner logs the distance (km) of each training run during a week. With arrays, the statistics are one call each:
import numpy as np
distances = np.array([5.2, 0.0, 7.5, 3.1, 10.0, 0.0, 12.4])
print("Total km: ", distances.sum())
print("Longest run: ", distances.max())
print("Run days: ", np.count_nonzero(distances))
print("Average (run days only):", distances[distances > 0].mean())Total km: 38.199999999999996
Longest run: 12.4
Run days: 5
Average (run days only): 7.639999999999999
The long decimals such as 38.199999999999996 are normal floating-point rounding (0.1 has no exact binary form); the Rounding Decimals lesson shows how to tidy them. Don't worry about distances[distances > 0] yet: that is boolean filtering, covered in the Array Filter lesson. For now, notice how readable the code is.
Getting help
Every NumPy function has a docstring. In a terminal or notebook you can call help(np.mean), or in Jupyter write np.mean?. Printing just the first line is a quick way to see what a function does:
import numpy as np
print(np.mean.__doc__.strip().splitlines()[0])
print(np.arange.__doc__.strip().splitlines()[0])Compute the arithmetic mean along the specified axis.
arange([start,] stop[, step,], dtype=None, *, device=None, like=None)
Tip: The official documentation at
numpy.org/dochas a reference page for every function, with examples. When you meet an unfamiliar function in this course, it is worth opening its page.
Test yourself
Which command installs NumPy with pip?
- pip get numpy
- pip install numpy
- python numpy install
- npm install numpy
What is the conventional alias for NumPy?
- nm
- num
- np
- npy
How do you check which NumPy version is installed?
- np.version()
- np.__version__
- numpy --v
- np.info.version