matrix
How to make List from Numpy Matrix in Python [duplicate]
May not be the optimal way to do this but the following works: a = numpy.matrix([[ 0.16666667, 0.66666667, 0.16666667]]) list(numpy.array(a).reshape(-1,)) or numpy.array(a).reshape(-1,).tolist() or numpy.array(a)[0].tolist()
Dynamic programming – Largest square block
Here is a sketch of the solution: For each of the cells we will keep a counter of how big a square can be made using that cell as top left. Clearly all cells with 0 will have 0 as the count. Start iterating from bottom right cell and go to bottom left, then go … Read more
Using a sparse matrix versus numpy array
The scipy sparse matrix package, and similar ones in MATLAB, was based on ideas developed from linear algebra problems, such as solving large sparse linear equations (e.g. finite difference and finite element implementations). So things like matrix product (the dot product for numpy arrays) and equation solvers are well developed. My rough experience is that … Read more
whats the fastest way to find eigenvalues/vectors in python?
**if your matrix is sparse, then instantiate your matrix using a constructor from scipy.sparse then use the analogous eigenvector/eigenvalue methods in spicy.sparse.linalg. From a performance point of view, this has two advantages: your matrix, built from the spicy.sparse constructor, will be smaller in proportion to how sparse it is. the eigenvalue/eigenvector methods for sparse matrices … Read more
How to convert a column or row matrix to a diagonal matrix in Python?
You can use diag method: import numpy as np a = np.array([1,2,3,4]) d = np.diag(a) # or simpler: d = np.diag([1,2,3,4]) print(d) Results in: [[1 0 0 0] [0 2 0 0] [0 0 3 0] [0 0 0 4]] If you have a row vector, you can do this: a = np.array([[1, 2, 3, … Read more
Calculate Matrix Rank using scipy
Numpy provides numpy.linalg.matrix_rank(): >>> import numpy >>> numpy.__version__ ‘1.5.1’ >>> A = numpy.matrix([[1,3,7],[2,8,3],[7,8,1]]) >>> numpy.linalg.matrix_rank(A) 3