Numpy#

Numpy is a library that brings array operations to Python. This page focuses on key library concepts.

import numpy as np
from IPython.display import HTML
header_template = "<text style='font-size:20px'>{}</text>"

Axis#

Array nesting is a numpy feature. Each level of nesting has a different number, starting from the outermost to the innermost.

Description#

The following cell shows the array form of 3 arrays, each having two arrays each having 4 elements:

arr = np.array([
    [
        [1,2,3,4],
        [3,3,2,1]
    ],
    [
        [3,2,1,3],
        [5,3,2,1]
    ],
    [
        [3,2,1,3],
        [5,3,2,1]
    ]
])
arr.shape
(3, 2, 4)

So the position on the shape corresponds to the axis:

  • axis 0 is the array of the two dimensional arrays;

  • axis 1 is the array of the one dimensional arrays;

  • axis 2 is the array of the end elements.

Aggregations#

There are many functions in numpy that somehow aggregate values along an axis - they usually take an axis argument. For example, we will consider the aggregation function numpy.mean.

The following cell applies np.mean along all possible axes.

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

display(HTML(header_template.format("axis=0")))
display(np.mean(arr, axis=0))
display(HTML(header_template.format("axis=1")))
display(np.mean(arr, axis=1))
display(HTML(header_template.format("axis=2")))
display(np.mean(arr, axis=2))
axis=0
array([[2.33333333, 2.        , 1.66666667, 3.33333333],
       [4.33333333, 3.        , 2.        , 1.        ]])
axis=1
array([[2. , 2.5, 2.5, 2.5],
       [4. , 2.5, 1.5, 2. ],
       [4. , 2.5, 1.5, 2. ]])
axis=2
array([[2.5 , 2.25],
       [2.25, 2.75],
       [2.25, 2.75]])

When aggregating along an axis using the np.mean function, we apply the function to all elements that have the same position on the other axes but vary along the specified axis. This means that for each set of elements aligned along the specified axis, the mean is computed and a single value is returned for that set, reducing the dimensionality of the array by one along the specified axis. So for:

  • axis=0 output size is (2,4) (no zero axis).

  • axis=1 output size is (3,4) (no one axis).

  • axis=2 output size is (3,2) (no two axis).

Create array#

There are many ways to create an array in numpy. Some summarization is represented in the following table.

Method

Function

Description

From list or tuple

np.array([...])

Creates a array from a list of lists (or tuples)

Zeros array

np.zeros((rows, cols))

Creates a array filled with zeros

Ones array

np.ones((rows, cols))

Creates a array filled with ones

Constant array

np.full((rows, cols), fill)

Creates a array filled with a constant value

Identity array

np.eye(n)

Creates an identity array of size n x n

Diagonal array

np.diag([a, b, c])

Creates a diagonal array with given values

Random uniform

np.random.rand(rows, cols)

Creates a array with random values in [0, 1)

Random normal

np.random.randn(rows, cols)

Creates a array with values from the standard normal distribution

Integer random array

np.random.randint(a, b, size)

Creates a array with random integers from a to b

Range reshaped

np.arange(n).reshape(r, c)

Creates a reshaped array from a range of numbers

Linearly spaced

np.linspace(start, stop, num)

Creates a 1D array of evenly spaced values (can be reshaped to array)

Tiled array

np.tile(array, reps)

Repeats a smaller array into a larger array

Empty array

np.empty((rows, cols))

Creates an uninitialized array (contents are arbitrary)

From string

np.fromstring(s, sep=' ')

Creates a array from a string of numbers (must reshape if 2D is needed)

From buffer

np.frombuffer(buffer)

Creates a array from a buffer object (e.g. bytes)

From function

np.fromfunction(func, shape)

Creates a array by evaluating a function over indices

For more information on some of the functions, check out the corresponding page.

Operations#

The following are different functions that perform various operations on NumPy objects.

Operation

NumPy Function / Method

Description

Matrix addition

np.add

Element-wise addition of two matrices

Matrix subtraction

np.subtract

Element-wise subtraction of two matrices

Element-wise multiplication

np.multiply

Element-wise multiplication of two matrices

Matrix multiplication

np.matmul / @

Matrix product (dot product)

Dot product

np.dot

Dot product of two arrays (for 2D, same as matmul)

Dot product for tensors

np.tensordot

Compute tensor dot product along specified axes

Sum of product with specified rues

np.einsum

Summation specified with Einstain summation convention

Transpose

np.transpose / .T

Transpose of a matrix

Determinant

np.linalg.det

Determinant of a square matrix

Inverse

np.linalg.inv

Inverse of a square matrix

Eigenvalues and eigenvectors

np.linalg.eig

Compute eigenvalues and eigenvectors

Singular value decomposition

np.linalg.svd

Singular value decomposition

Concatenate matrices

np.concatenate

Join matrices along an axis

Stack matrices vertically

np.vstack

Stack matrices in sequence vertically (row-wise)

Stack matrices horizontally

np.hstack

Stack matrices in sequence horizontally (column-wise)

Reshape matrix

np.reshape

Change the shape of a matrix

Flatten matrix

np.ravel / np.flatten

Flatten matrix to 1D array (ravel returns view if possible)

Repeat elements

np.repeat

Repeat elements of an array

Tile matrix

np.tile

Construct an array by repeating a matrix

Sum of elements

np.sum

Sum of all or specific axis elements

Mean of elements

np.mean

Mean of all or specific axis elements

Max element

np.max

Maximum of all or specific axis elements

Min element

np.min

Minimum of all or specific axis elements

Element-wise comparison

np.equal, np.greater, etc

Compare matrices element-wise

Solve linear system

np.linalg.solve

Solve a linear matrix equation Ax = b

Tensor dot product

np.tensordot

Sum product over specified axes of tensors

Broadcasting

Automatic via NumPy rules

Implicit extension of shapes for element-wise operations

For more details on specific operations, check out the operations page.

Combinations#

Consider task of getting all combinations of the elements of a particular numpy array. Suppose we have to arrays:

\[(x_1, x_2, \ldots, x_n)\]
\[(y_1, y_2, \ldots, y_m)\]

Finally, we are interested in obtaining an array containing:

\[\begin{split} \left(\begin{array}{cc} x_1 & y_1 \\ x_1 & y_2 \\ \vdots & \vdots \\ x_1 & y_m \\ x_2 & y_1 \\ x_2 & y_2 \\ \vdots & \vdots \\ x_2 & y_m \\ \vdots & \vdots \\ x_n & y_1 \\ x_n & y_2 \\ \vdots & \vdots \\ x_n & y_m \end{array}\right) \end{split}\]

There is a line for each combination of the elements under consideration.

In numpy terms in case we need to get combinations of the arrays a and b we supposed to use syntax:

np.array(np.meshgrid(a, b)).T.reshape(-1, 2)

It generates a really simple example and applies the transformations described earclier to it one by one.

a = np.array([1, 2, 3])
b = np.array([-1, -2, -3])
mesh_grid = np.meshgrid(a, b)
joined = np.array(mesh_grid)
joined
array([[[ 1,  2,  3],
        [ 1,  2,  3],
        [ 1,  2,  3]],

       [[-1, -1, -1],
        [-2, -2, -2],
        [-3, -3, -3]]])
joined.T.reshape(-1, 2)
array([[ 1, -1],
       [ 1, -2],
       [ 1, -3],
       [ 2, -1],
       [ 2, -2],
       [ 2, -3],
       [ 3, -1],
       [ 3, -2],
       [ 3, -3]])