Cheatography
https://cheatography.com
Variety of functions and useful things with a special focus on linear algebra/matricial stuff.
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Libraries
Numpy |
|
Linear algebra (numpy) |
import numpy.linalg as LA
|
Sparse operations (scipy) |
import scipy.sparse as ss
|
Sparse normalizations (sklearn) |
from sklearn.preprocessing import normalize as sparse_norm
|
Array comparisons
Locate indices based on condition |
index = np.where(array == 4)
|
Check if all/any satisfy condition |
np.any(array == 1), np.all(array1 == array2)
|
Matricial operations
Dot product |
np.dot(v1, v2)
or v1 @ v2
|
Outer product |
|
Sum rows (0), columns (1) |
|
Eigenvector, eigenvalues |
eigval, eigvec = LA.eig(A)
|
The eigenvector output of the eig()
function is a matrix containing the usual eigenvectors as its columns.
Numpy comparisons
Check if any element satisfies a condition |
|
Find which elements (their indices) satisfy a condition |
|
Check if all elements satisfies a condition |
|
Closeness up to tolerance |
|
Numpy utilities
Arrange all array elements in a list |
|
|
|
Sparse matrices with scipy
Convert numpy matrices to sparse |
A_sparse = ss.csr_matrix(A) A_sparse = ss.csc_matrix(A)
|
Change CSC to CSR and viceversa |
A_csr = A_csc.tocsr() A_csc = A_csr.tocsc()
|
Row/col. normalization |
A_norm = sparse_normalize(A_csc, norm='l1', axis=1)
|
Sparse identity matrix |
sparse_id = ss.identity(n)
|
Invert a (CSC) sparse matrix |
sparse_inv = ss.inv(A_sparse)
|
Dot product with vector |
|
Transpose sparse matrix |
At_sparse = A_sparse.transpose()
|
CSC (Compressed Sparse Column) are more suitable for column operations while CSR (Compressed Sparse Row) are for row operations. For instance, row/col. normalizations.
|