Numpy tutorial
blockquote not implementedReference
Scipy official reference: http://docs.scipy.org/
Scipy Lecture Notes: http://www.scipy-lectures.org/
Python Data Science Handbook: https://jakevdp.github.io/PythonDataScienceHandbook/
Python course .eu: https://www.python-course.eu/numpy.php
Call the function
np.func(a, x, y)
is the same as a.func(x, y)
.
Convention
The common short name for numpy
is np
.
import numpy as np
np.__version__
Creating an array from a sequence
array(seq)
, asarray(seq)
seq
can be a tuple, list, or a numpy array.
asarray()
does not make a new copy if seq is already a numpy array.
Creating a 1D numpy array from a list.
Array basics
a.ndim
: number of dimensionsa.shape
: Tuple of lengths for each dimensiona.size
: total size (product of shape) =len(a)
a.dtype
: data typea[i]
: accessing i th element in the 1D array (start from 0)a[i, j]
: accessing element i th row, j th column (2D array, start from 0)a[:, j]
: accessing j th column(2D array)a[i, :]
: accessing j th row (2D array)a.T
: Transpose ofa
a.copy()
: make a copy of a that does not share memorya.reshape(shape)
: reshape the array if the new size is compatible (i.e. the same total size)
a = np.array([1, 9, 8, 7])
a
a.ndim
a.shape
a.size
a.dtype
a[3]
For complex numbers, j
being the imaginary part.
np.array([1+2j, 3+4j, 5+6*1j]).dtype
Creating a multidimensional array from a nested list , with complex numbers
b = np.asarray([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0+1j]])
b
b.ndim
b.shape
b.dtype
b[1, :]
b[:, 0]
b.T
b.reshape((3, 2))
b.reshape((1, -1)) # -1 mean caculate dim automatically
Creating an array from a function
arange(start, stop, step)
linspace(start, stop, num, endpoint=True)
logspace(start, stop, num, endpoint=True)
ones((d1, d2, ...))
ones_like(arr)
zeros((d1, d2, ...))
zeros_like(arr)
full((d1, d2, ...), val)
eye(k)
diag(seq)
fromfunction(f, (d1, d2, ...))
fromiter(iter)
meshgrid(x1, x2, ...)
np.arange(10, 0, -1)
np.linspace(0, 1, 5)
np.linspace(0, 1, 5, endpoint=False)
np.logspace(-10.0, 10.0, 11)
np.ones((3, 3))
a = np.arange(5.0)
print(np.ones_like(a))
np.full((3, 4), 42)
np.full_like(a, 69)
np.eye(3)
np.diag([4, 5, 6, 7])
np.fromfunction(lambda i, j: i >= j, (4, 4))
np.fromiter((x*x for x in range(5)) , dtype=np.float)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
# sparse=True to save some memory
xx, yy = np.meshgrid(x, y, sparse=True)
print('xx =', xx, sep='\n')
print('yy =', yy, sep='\n')
plt.contourf(x,y, np.sin(xx**2 + yy**2) / (xx**2 + yy**2))
plt.colorbar()
Random
The new API: https://numpy.org/doc/stable/reference/random/index.html?highlight=random#module-numpy.random
from numpy.random import default_rng
rng = default_rng()
rng.random()
# Uniform [0, 1)
rng.random((4, 3))
# Integers
rng.integers(1, 7, (10, 20))
# Standard uniform distribution
rng.standard_normal(10)
# Random choice
choices = np.array(["one", "two"])
# Select by index
choices[rng.integers(0, 2, (3, 4))]
# Or choice function, prob weights supported
rng.choice(choices, size=(5, 3), p=[0.3, 0.7])
Selecting elements
a = np.arange(10)
a
# a[idx]
a[0], a[3], a[7]
# a[[indices]]
a[[0, 3, 7]]
# a[condition]
# Selection from an array of true/false value
a[a<5]
# Slice: a[start:end:step]
a[1::2]
# Reverse
a[::-1]
# Mutating the elements
a[0] = 1000
a
Indexing for 2D / 3D arrays
In 2D, the first dimension corresponds to rows, the second to columns. Numpy is row-major by default, as in C-styled arrays.
a[i, j]
for the element from ith row and jth column.
b = np.arange(25).reshape((5,5))
b
# Each index is separated by comma
b[2, 3]
# Slices share the same underlying object of the original.
c = b[1::2, 1::2]
c
c[0, 0] = 666 # Mutates b !!!
print("After mutating:")
b
np.may_share_memory(c, b)
# Use copy to prevent unwanted overwriting
a = np.arange(10)
c = a[::2].copy()
c[0] = 12
a
# combining assignment and slicing
a = np.arange(10)
a[5:] = 10
a
b = np.arange(5)
a[5:] = b[::-1]
a
Numerical operations on arrays
Element-wise (broadcasting) operations by default.
Some math functions could be found in numpy (e.g. sin, cos): use
np.lookfor(desc.)
Others could be found in scipy documentations.
a = np.arange(10)
a
a+1
a-3
a*2
a/4
2**a
With an array: Only if dimension sizes are compatible: either the same or 1.
a = np.array([[1, 2, 3, 4],
[5, 6, 7, 8]])
b = rng.random((2, 4))
a+b
a-b
a*b
a/b
np.sin(b)
a = np.array([1, 2, 3, 4])
b = np.array([4, 2, 2, 4])
a == b
a = np.arange(1, 10)
b = np.arange(1, 8).reshape((-1, 1))
a
b
# Broadcasting: A(1*M) * B(N*1) = C(N * M)
a*b
Matrix multiplication
dot(a, b)
, a@b
a = rng.random((5, 5))
b = rng.random((5, 5))
# Element-wise multiplication
a*b
# Matrix multiplication
a@b
# Matrix multiplication
np.dot(a,b)
# No need to transpose 1D array for `dot(a, b)`
a = rng.random((5, 5))
b = rng.random(5)
c = rng.random(5)
# Matrix x vector
np.dot(a,b)
# Vector * vector
np.dot(c,b)
Combing Arrays
This one will give your headaches.
concatenate((a, b), axis=n)
stack((a,b), axis=n)
The former joins arrays in the existing axis; the latter creates a new axis.
a = np.arange(0, 10)
b = np.arange(0, 10) + 10
# along the row (1st axis), existing axis
np.concatenate((a, b), axis=0)
# along the column (2nd axis)
np.stack((a, b), axis=1)
Reduction
sum(v, axis=n)
, cumsum(v, axis=n)
a = np.arange(0, 6).reshape((2, 3))
a
np.sum(a)
np.sum(a, axis=1)
np.sum(a, axis=0)
np.cumsum(a)
np.cumsum(a, axis=1)
np.cumsum(a, axis=0)
amin(v, axis=n)
amax(v, axis=n)
minimum(a, b)
maximum(a, b)
argmin(v, axis=n)
argmax(v, axis=n)
np.amin(a)
np.argmin(a)
np.amax(a)
np.argmax(a)
b = (rng.standard_normal((2, 3)) + 1) * 5
b
np.minimum(a, b)
np.maximum(a, b)
np.all([True, True, False])
np.any([True, True, False])