I created matrix by np.zeros()
, and tested it with sp.issparse()
. Why I get false even the matrix is zero (or in case of mostly zero matrix as well)?
Asked
Active
Viewed 1,151 times
2

Jan
- 1,389
- 4
- 17
- 43
-
This `issparse` tests the matrix class, not the proportion of 0s – hpaulj Jun 07 '17 at 06:52
-
The code for this function is just: `isinstance(x, sparse.spmatrix)` – hpaulj Jun 07 '17 at 07:32
1 Answers
3
A sparse matrix has a different memory representation: it is not the cells being zero that matters but the fact that the cells are allocated in memory.
np.zeros()
allocates all fields of the array in memory, even those being zero, while a sparse matrix does not.
See here for how to convert from a numpy matrix/array to a sparse matrix.

akoeltringer
- 1,671
- 3
- 19
- 34