Incomplete sheet
This sheet is incomplete and could use some attention. Please submit code snippet suggestions as an issue or PR here.
Matrices
2D-array operations
Create
| Action |
Code |
Details |
|
Undefined (size n x m)
|
|
Warning: don't use the initial values! |
|
Zeros (size n x m)
|
|
|
|
Ones (size n x m)
|
|
|
|
True values (size n x m)
|
np.full((n, m), fill_value=True)
|
|
|
False values (size n x m)
|
np.full((n, m), fill_value=False)
|
|
|
Fill with value v (size n x m)
|
np.full((n, m), fill_value=v)
|
|
|
Identity matrix of size n x m
|
|
|
|
Diagonal matrix from vector
|
|
|
|
From vectors as rows
|
|
|
|
From vectors as rows
|
|
|
|
From vectors as rows
|
|
|
|
From vectors as columns
|
np.column_stack((v1, v2))
|
|
|
From vectors as columns
|
np.stack((v1, v2), axis=1)
|
|
|
From vectors as columns
|
np.vstack((v1, v2)).transpose()
|
|
| Action |
Code |
Details |
|
Number of elements
|
|
|
|
Hash
|
|
|
|
Element at row i, column j
|
|
|
Update
All operations are in-place
| Action |
Code |
Details |
|
Update element at row i, column j
|
|
|
|
Fill with scalar value
|
|
|
|
Fill with array values
|
|
|
|
Fill row i with scalar value
|
|
|
|
Fill column j with scalar value
|
|
|
Reshape
| Action |
Code |
Details |
|
Transpose
|
|
|
|
Resize to shape, fill with zeros
|
|
|
Derive
All operations create a new instance
Map
Operations are element-wise and preserve the shape of the matrix.
| Action |
Code |
Details |
|
Increment all elements with scalar value
|
|
|
|
Increment elements by the respective element of another matrix
|
|
Must have same shape |
|
Find min between two matrices
|
|
|
|
Find max between two matrices
|
|
|
|
Clip (truncate) between [a, b]
|
np.clip(m, a_min=a, a_max=b)
|
a_min < a_max is not checked |
Order
Operations preserve the shape of the matrix.
| Action |
Code |
Details |
|
Reverse elements
|
|
Flattened view in reverse order |
|
Sort elements descending
|
|
NaNs are last |
|
Sort elements by column descending
|
|
|
|
Sort elements per row descending
|
|
|
|
Reverse column order
|
|
|
|
Reverse row order
|
|
|
Operations that increase the size of the matrix
| Action |
Code |
Details |
|
Pad with sclar value
|
np.pad(m, pad_width=1, constant_values=v)
|
|
|
Pad with edge element
|
np.pad(m, pad_width=1, mode='edge')
|
|
Shrink
Operations that reduce the shape of the matrix
| Action |
Code |
Details |
|
Diagonal vector
|
|
|
Aggregate
Summarize along an axis. Set axis=1 for per-row operation.
| Action |
Code |
Details |
|
Min per column
|
|
Use nanmin() to ignore NaNs |
|
Max per column
|
|
Use nanmax() to ignore NaNs |
|
Max - min, per column
|
|
|
|
Sum per column
|
|
Use nansum() to ignore NaNs |
|
Mean per column
|
|
Use nanmean() to ignore NaNs |
|
Median per column
|
|
Use nanmedian() to ignore NaNs |
Reshape
| Action |
Code |
Details |
|
Transpose
|
|
|
|
Transpose
|
|
|
|
Rotate
|
|
|
|
Reshape to specified dimensions
|
|
|
Convert
| Action |
Code |
Details |
|
1D array (concat rows)
|
|
|
|
1D array (concat columns)
|
|
More intuitive than specifying mode |
|
1D array (concat columns)
|
|
|
|
List of rows
|
|
|