NumPy essentials

providing mathematical functions for the Python programmer
2025-11-17 15:13
// updated 2026-01-06 16:07

NumPy (numpy.org) refers to a Python library that specializes in mathematics and works well especially with large multi-dimensional arrays (aka matrices):

Setup

Installation

$ pip install numpy

"Hello, world!"

In a file like app.py, import the NumPy library:

# include this once per file
import numpy as np

Code further in the same file:

a = np.array([1, 2, 3])
print("basic numpy array: ")
print(a)

Runtime

Then, in the command line (repeat this for all examples below):

$ python app.py

Output:

basic numpy array: 
[ 1 2 3 ]

Tensors

In Python, a simple list actually just refers to a 1-D tensor; nested lists allow us to represent higher dimensional tensors:

2-D tensors (matrices)

m = np.array([[1,2,3],[4,5,6]])

print("basic numpy matrix, i.e. a 2D list:  ")
print(m)

Output:

basic numpy matrix, i.e. a 2D list:
[[1 2 3]
 [4 5 6]]

3-D tensors

t = np.array([[[1, 1, 1], [2, 2, 2], [3, 10, 11]], 
              [[4, 4, 4], [5, 5, 5], [6, 12, 13]], 
              [[7, 7, 7], [8, 8, 8], [9, 14, 15]]])

print("basic numpy tensor, i.e. a 3D list:  ")
print(t)

Output:

basic numpy tensor, i.e. a 3D list:
[[[ 1  1  1]
  [ 2  2  2]
  [ 3 10 11]]

 [[ 4  4  4]
  [ 5  5  5]
  [ 6 12 13]]

 [[ 7  7  7]
  [ 8  8  8]
  [ 9 14 15]]]

Mappings

i.e. performing an operation on all the values of a list:

t_mapped = t * 2
print("multiplying the previous 3-D tensor by 2: ")
print(t_mapped)

Output:

multiplying the previous 3-D tensor by 2: 
[[[ 2  2  2]
  [ 4  4  4]
  [ 6 20 22]]

 [[ 8  8  8]
  [10 10 10]
  [12 24 26]]

 [[14 14 14]
  [16 16 16]
  [18 28 30]]]

Dimensionality

using the ndim property of a list to get the number of dimensions:

print("getting the dimension of an array: ")
print("array (a):", a.ndim)
print("matrix (m):", m.ndim)
print("3D tensor (t):", t.ndim)

Output:

getting the dimension of an array:
array (a): 1
matrix (m): 2
3D tensor (t): 3

Elemental operations

Accessing elements by index

For a reference like t[1, 2, 3] we would refer to the outermost list with the first index and the innermost list as the last index ("working our way in")!

Also recall that lists are 0-based so that t[0, 0, 0] refer to the first element of each dimension of the list:

import numpy as np
my_3dlist = np.array(
[
  [
    [7, 2], 
    [1, 4]
  ], 
  [
    [3, 0], 
    [-1, -2]
  ], 
  [
    [9, 5], 
    [8, 6]
  ]
]
                    )
print(my_3dlist[0])
print(my_3dlist[0, 1])
print(my_3dlist[0, 1, 1])

(The indentation is intentional as a visual aid!)

Output:

[[7 2]
 [1 4]]

 [1 4]

 4

Updating elements by index

Use the same notation as with accessing an element but like a variable:

tensor[index1, index2, ..., indexN] = value

i.e. and e.g.:

tensor[3, 2, 6, 2] = 100

Creating a matrix with random elements

We can make use of the random property with the randint method to create an m x n matrix with elements ranging from x to y:

x = -10
y = 10
m = 4
n = 3

m_random = np.random.randint(x, y, size=(m, n))

Statistical operations

Some examples of statistical operations that we can perform on elements of a tensor, t, include:

# maximum
np.max(t)

# minimum
np.min(t)

# mean (average)
np.mean(t)

# median
np.median(t)

# sum of all elements
np.sum(t)

# standard deviation of all elements
np.std(t)
⬅️ older (in snippets)
🐍📚 Using Python libraries
newer (in snippets) ➡️
Pre-processing steps for NLP 📜⚙️📃
⬅️ older (posts)
🐍📚 Using Python libraries
newer (posts) ➡️
Pre-processing steps for NLP 📜⚙️📃