Numpy

Important types

Arrays

Important ndarray attributes:

Creation

array = np.array(Iterable, [dtype=...], copy=True)

The type of the array’s elements is optional and will usually be deduced as one of int64, float64.

Sequences of sequences will get transformed to 2D arrays. Can keep nesting sequences to increase the number of dimensions.

If shape = (dim0,dim1,...) is a tuple:

Useful methods

Indexing

# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

Slicing, and integer array indexing. Returns a view.

a[1, :]  # [5 6 7 8] of shape (4,)
a[1:2, :] # [[5 6 7 8]] of shape (1, 4)
a[[1,2], [0,1]] # [[5 6], [9 10]]

Can also use ndarrays to index.

Boolean array indexing

a[a < 3] # [1 2]
a[(a > 2) & (a < 5)]
# & (and), | (or), ^ (xor), ~ (not)

Conversions

By default, 1D arrays are treated as row vectors ((n,), (1,n)) in 2D operations. To convert a 1D array to a column vector, use e.g.

Transpose (.T) can be used to switch between row and column 2D vectors.

Operations

When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (upcasting).

Universal Functions

Universal functions are instances of the numpy.ufunc class.

Operates on ndarrays element-wise (ufunc). Supports broadcasting.

Examples

https://numpy.org/devdocs/reference/ufuncs.html

Can create your own ufunc by using np.frompyfunc.

Methods

Methods on the ufunc itself. These methods only make sense on scalar ufuncs that take two input arguments and return one output.

Routines

Prefer functions defined on the array object where possible. Less Python overhead, though might work in-place.

Functions on Array

By default axis=None. For a_ij: i is axis=0, and j is axis=1. So axis 0 refers to the rows e.g. array.sum(axis=0) gives the row-wise sum of array.

Functions on Module

Suppose a = [1, 2, 3, 4]

Set routines

unique, counts = np.unique(x, return_counts=True)

Array Manipulation

Single array

Joining arrays

Linear Algebra

Random

from numpy.random import default_rng
rng = default_rng(seed)
vals = rng.standard_normal(10)
more_vals = rng.standard_normal(10)

rng.binomial(n, p[, size])
rng.exponential([scale, size])
rng.normal(loc=0, scale=1, size=None)
rng.uniform(low=0, high=1, size=None)

rng.shuffle(arr)
rng.integers(low_incl, high_excl, size=None|shape) # prefer to randint
rng.choice(a, size=None, replace=True, shuffle=True)

https://numpy.org/devdocs/reference/random/generator.html

Polynomials

from numpy.polynomial import Polynomial as poly
p1 = poly([coefs])
p2 = poly.fit(x,y,deg)
p3 = poly.fromroots([roots])

p1(x) # evaluate
p1 '+-/*' p2
p.roots()

# calculus: n is nr of times to apply
p.integ(n, lbnd=-1, k=1) # k is the integ constant
p.deriv(n)

DateTime

import numpy as np
date = np.datetime64('2020-07-04')
date = np.datetime64('2020', 'D') # 2020-01-01
date_array = np.array('2020-07-04', dtype=np.datetime64)
one_day = np.timedelta64(1, 'D')

date +-/ one_day

Save and Loading Numpy Objects

np.save('filename', a)
b = np.load('filename.npy')
np.savetxt('filename.csv', a)
np.loadtxt('filename.csv')
np.savez_compressed('filename', a)

np.array2string(a, max_line_width=10)

.npy and .npz files are smaller and faster to read.

Help Functions

All Numpy Routines

See Also