This is a draft cheat sheet. It is a work in progress and is not finished yet.
Operataing element by element
Operate n to all components |
vector + n matrix n vector * n
|
Operation using another vector |
vector + vector matrix * matrix
|
Matrix : Transpose |
|
Matrix multiplication |
np.dot(matrix,matrix.T)
|
Vectors & Matrix
Create vector from list |
|
Create vector of n zeros |
|
Create vector of n ones |
|
Create vector of type float32 |
np.array([1,-0.3,2], dtype=np.float32)
|
Change type of a vector |
vector = vector.astype(np.uint32)
|
Create matrix of ones |
|
Create matrix from other matrix shape |
m2 = np.zeros_like(m1)
|
Reshape a matrix (constant size) |
mat=mat.reshape((6,3))
|
|
|
Matrix & Vectors Operations
Sum of all components |
np.sum(vector) vector.sum()
|
Maximum |
np.max(vector) vector.max()
|
Minimum |
np.min(vector) vector.min()
|
Product |
np.prod(vector) vector.prod()
|
Operation over column |
|
Operation over row |
|
Absolute |
|
Indexing
Especific element |
|
Range of row / columns |
|
Submatrix (same matrix) |
submatrix = matrix[1:3, 1:3]
|
Submatrix (Different matrix) |
submatrix = submatrix.copy()
|
Boolean Indexing |
Finding negative values |
negatius = example < 0 |
Selecting negative values |
example[negatius] |
Changing values directly |
example[example==0] = 0 |
|
|
Broadcasting
Normalize a matrix by columns |
np.random.uniform(size=(3,4))
sumes = matrix.sum(axis=0) norm = matrix/sumes
|
Normalize a matrix by rows** |
norm=(matrix.T/sumes).T norm=matrix/sumes.reshape((3,1))
|
Not-a-Number ( Nan) |
Initialize as NaN |
`a = np.nan |
Test if is NaN |
|
Test also with vectors |
|
Extra
Random uniform matrix |
np.random.uniform(size=(3,4))
|
Another type of random matrix |
np.random.uniform(-1,1,size=(3,4))
|
Copying a matrix |
|
Shape of a matrix |
|
Data types |
dtype = np.float32
dtype = np.float64
dtype = np.int32
dtype = uint32
dtype = np.int8
dtype = np.int8
dtype = np.uint8
|
Importing |
|
|