A NumPy Reference
Many of my articles use NumPy and I usually include a brief overview of the library, its installation process and basic usage. To avoid the necessity of repeating this in every article I have written this short introductory guide to serve as a general reference for all my projects using NumPy.
What is NumPy?
NumPy is a third-party Python library the central feature of which is the ndarray (n-dimensional array) data structure. The ndarray is way faster than a Python list as well as being easier to use and providing a wide range of methods and other functionality. It is very widely used and to me is an indispensible part of Python which I use regularly.
NumPy's best known use case is in data science but it is applicable to pretty much any field as well as being the basis of more specialised libraries such as Pandas, SciPy and many more.
Despite its power and flexibility it's easy to get up and running with NumPy as the examples below illustrate. Using the library for more complex tasks is also straightforward and in my opinion is best learned on the fly as and when you need more functionality. However, it's worth skimming the documentation to see what is available and to spot anything that might be particularly useful to you.
How Do I Install NumPy?
Numpy "lives" on PyPI, the Python Package Index, so can be installed using pip:
pip install numpy
This is the NumPy page on PyPI:
https://pypi.org/project/numpy/
Basic NumPy Usage
The code snippets below all illustrate a specific aspect of using NumPy. If you want to try them out the easiest way is to run Python interactively in your terminal/console by typing python (plus optionally a version number, eg. python3.12) and hitting enter. You can then enter or paste snippets of Python code and run them by pressing enter.
When running Python in the terminal you can just enter a variable name and it will be printed and that is how these examples are written. However, if you copy them to a .py code file you'll need to use print().
import
Being a third-party library NumPy has to be imported in every .py file that uses it, and also in a console. It is standard practice to import it as np.
import numpy as np
__version__
If you need to know the version you have installed it's easy to find.
np.__version__
NumPy array from a list
If you have an ordinary list, hard-coded or runtime-generated, you can create a NumPy array from it like this.
n = np.array([3,5,8,12,14,15,19,29,33])
n
Creating a Range of Values
The arange method populates a new array with values between the specified min and max, and with the specified interval.
degrees = np.arange(0,181,10)
degrees
Operations on a Whole Array
A very useful feature of NumPy is that you can carry out operations on an entire array with one line of code, iteration being abstracted away out of sight. The following examples add 1 to all elements, multiply all elements by 2, and square all elements.
n = np.array([1,2,3,4,5,6,7,8])
n
plusone = n + 1
plusone
timestwo = n * 2
timestwo
squared = n**2
squared
Statistics
NumPy has a large number of mathematical and statristical methods which can be applied to an array. These examples demonstrate the sum and mean methods. (The mean is the arithmetic mean, commonly known as the "average". ie. the sum divided by the number of values.) Note that you can use either np.sum(data) or data.sum().
data = np.array([122,33,45,67,98,1,45,80])
data
sum = np.sum(data)
sum
data.sum()
mean = np.mean(data)
mean
data.mean()
Getting the Size of an Array
This snippet shows how to get the size of an array. (All the example arrays here are one-dimensional but if you use size on a higher-dimensional array it returns the total number of elements in all dimensions. You can get a tuple containing the sizes of all dimensions with the shape method.)
n = np.arange(0,200,0.75)
np.size(n)
np.shape(n)
Indexing
NumPy arrays use the normal Python indexing syntax.
n = np.array([122,33,45,67,98,1,45,80])
n
n[4] # 5th element
n[2:5] # 3rd to 5th elements
n[-1] # last element
Filtering
NumPy's where method returns the indexes of values with the specified filter criteria not the values themselves. Getting the values is therefore a two-step process but it's easy to combine these as in the last line of code.
n = np.arange(0,320,10)
(n)
indexes = np.where(n>=160)
indexes
n[indexes]
n[np.where(n>=160)]
Further Information
This article is intended as a very brief get-you-started guide for anyone who has not used NumPy before. The library has a vast amount of functionality, some of it very specialised. It is very well documented so if you need more information just head to the official site.
At the bottom of the home page there is an interactive shell where you can type in and run snippets of Python, including of course code using NumPy. Personally I prefer running Python in the terminal but you might like to give this interactive shell a try.
If I use any NumPy functionality not covered here in any of my future projects I'll explain it in the article, and also update this page if it's something likely to be widely used.