2

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)?

2
  • This issparse tests the matrix class, not the proportion of 0s Commented Jun 7, 2017 at 6:52
  • The code for this function is just: isinstance(x, sparse.spmatrix) Commented Jun 7, 2017 at 7:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.