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))
array([[2.33333333, 2. , 1.66666667, 3.33333333],
[4.33333333, 3. , 2. , 1. ]])
array([[2. , 2.5, 2.5, 2.5],
[4. , 2.5, 1.5, 2. ],
[4. , 2.5, 1.5, 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 |
|
Creates a array from a list of lists (or tuples) |
Zeros array |
|
Creates a array filled with zeros |
Ones array |
|
Creates a array filled with ones |
Constant array |
|
Creates a array filled with a constant value |
Identity array |
|
Creates an identity array of size |
Diagonal array |
|
Creates a diagonal array with given values |
Random uniform |
|
Creates a array with random values in [0, 1) |
Random normal |
|
Creates a array with values from the standard normal distribution |
Integer random array |
|
Creates a array with random integers from |
Range reshaped |
|
Creates a reshaped array from a range of numbers |
Linearly spaced |
|
Creates a 1D array of evenly spaced values (can be reshaped to array) |
Tiled array |
|
Repeats a smaller array into a larger array |
Empty array |
|
Creates an uninitialized array (contents are arbitrary) |
From string |
|
Creates a array from a string of numbers (must reshape if 2D is needed) |
From buffer |
|
Creates a array from a buffer object (e.g. bytes) |
From function |
|
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 |
|
Element-wise addition of two matrices |
Matrix subtraction |
|
Element-wise subtraction of two matrices |
Element-wise multiplication |
|
Element-wise multiplication of two matrices |
Matrix multiplication |
|
Matrix product (dot product) |
Dot product |
|
Dot product of two arrays (for 2D, same as |
Dot product for tensors |
Compute tensor dot product along specified axes |
|
Sum of product with specified rues |
Summation specified with Einstain summation convention |
|
Transpose |
|
Transpose of a matrix |
Determinant |
|
Determinant of a square matrix |
Inverse |
|
Inverse of a square matrix |
Eigenvalues and eigenvectors |
|
Compute eigenvalues and eigenvectors |
Singular value decomposition |
|
Singular value decomposition |
Concatenate matrices |
|
Join matrices along an axis |
Stack matrices vertically |
|
Stack matrices in sequence vertically (row-wise) |
Stack matrices horizontally |
|
Stack matrices in sequence horizontally (column-wise) |
Reshape matrix |
|
Change the shape of a matrix |
Flatten matrix |
|
Flatten matrix to 1D array ( |
Repeat elements |
|
Repeat elements of an array |
Tile matrix |
|
Construct an array by repeating a matrix |
Sum of elements |
|
Sum of all or specific axis elements |
Mean of elements |
|
Mean of all or specific axis elements |
Max element |
|
Maximum of all or specific axis elements |
Min element |
|
Minimum of all or specific axis elements |
Element-wise comparison |
|
Compare matrices element-wise |
Solve linear system |
|
Solve a linear matrix equation |
Tensor dot product |
|
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:
Finally, we are interested in obtaining an array containing:
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]])