kuniga.me > Docs > Numpy Cheatsheet
Assuming numpy is imported as:
import numpy as np
Un-initialized vector of size N:
np.empty(shape=N)
Similar versions for vectors filled with 1s: ones
, ans 0s: zeros
. Note that np.empty(shape=N)
is different from np.empty(shape=(N, 1))
. The former has x[N]
, while the latter x[N, 1]
.
To flatten a matrix m(Nx1)
to a vector v(N)
:
v = m.reshape(shape=N)
Un-initialized matrix of size N x M:
m = np.empty(shape=(N, M))
Similar versions for vectors filled with 1s: ones
, and 0s: zeros
.
Number of rows:
len(m)
Number of columns:
len(m[0])
Initialize from nested Python lists:
m = np.array([
[0.1, 0.3, 0.5],
[0.9, 0.7, 0.5],
])
Transpose:
t = m.transpose()
Einstein Summation:
This article is a great introduction:
https://ajcr.net/Basic-guide-to-einsum/