NumPy and SciPy – Difference between .todense() and .toarray()
toarray returns an ndarray; todense returns a matrix. If you want a matrix, use todense; otherwise, use toarray.
toarray returns an ndarray; todense returns a matrix. If you want a matrix, use todense; otherwise, use toarray.
The scipy.sparse.*_matrix has several useful methods, for example, if a is e.g. scipy.sparse.csr_matrix: a.toarray() or a.A – Return a dense ndarray representation of this matrix. (numpy.array, recommended) a.todense() or a.M – Return a dense matrix representation of this matrix. (numpy.matrix)
Sorry, don’t have the reputation to comment, but I think you should take a look at the answer here: Keras, sparse matrix issue. I have tried it and it works correctly, just one note though, at least in my case, the shuffling led to really bad results, so I used this slightly modified non-shuffled alternative: … Read more
I replied over at scipy.org as well, but I thought I should add an answer here, in case others find this page when searching. You can turn the vector into a sparse diagonal matrix and then use matrix multiplication (with *) to do the same thing as broadcasting, but efficiently. >>> d = ssp.lil_matrix((3,3)) >>> … Read more
You can use the scipy.sparse.hstack to concatenate sparse matrices with the same number of rows (horizontal concatenation): from scipy.sparse import hstack hstack((X, X2)) Similarly, you can use scipy.sparse.vstack to concatenate sparse matrices with the same number of columns (vertical concatenation). Using numpy.hstack or numpy.vstack will create an array with two sparse matrix objects.
Maybe this explanation can help understand the concept: data is an array containing all the non zero elements of the sparse matrix. indices is an array mapping each element in data to its column in the sparse matrix. indptr then maps the elements of data and indices to the rows of the sparse matrix. This … Read more
Edit: bbtrb’s method (using coo_matrix) is much faster than my original suggestion, using nonzero. Sven Marnach’s suggestion to use itertools.izip also improves the speed. Current fastest is using_tocoo_izip: import scipy.sparse import random import itertools def using_nonzero(x): rows,cols = x.nonzero() for row,col in zip(rows,cols): ((row,col), x[row,col]) def using_coo(x): cx = scipy.sparse.coo_matrix(x) for i,j,v in zip(cx.row, cx.col, … Read more
This problem has a very efficient (linear time) solution, though it requires a bit of discussion… Zeroth: clarifying the problem / LCP Per clarifications in the comments, @FooBar says the original problem is elementwise min; we need to find a z (or v) such that either the left argument is zero and the right argument … Read more
Use a scipy.sparse format that is row or column based: csc_matrix and csr_matrix. These use efficient, C implementations under the hood (including multiplication), and transposition is a no-op (esp. if you call transpose(copy=False)), just like with numpy arrays. EDIT: some timings via ipython: import numpy, scipy.sparse n = 100000 x = (numpy.random.rand(n) * 2).astype(int).astype(float) # … Read more