1

I wish to add a dimension to a sparse matrix. In numpy it's simply a matter of doing [:,None]. I tried reshape and resize without any success.

Here's some dummy data:

from scipy.sparse import csr_matrix

data = [1,2,3,4,5,6] 
col = [0,0,0,1,1,1] 
row = [0,1,2,0,1,2] 
a = csr_matrix((data, (row, col)))
a.reshape((3,2,1))

The last line gives the error: ValueError: matrix shape must be two-dimensional. Doing resize instead gives the error ValueError: shape must be a 2-tuple of positive integers.

In my particular case I also need to reshape it to (3,1,2). Any thoughts?

2
  • 2
    Scipy sparse matrices are always 2d. There's no adding or removing dimensions! Commented Jul 7, 2020 at 3:35
  • 2
    First line in the scipy sparse docs: SciPy 2-D sparse matrix package for numeric data. Commented Jul 7, 2020 at 3:50

2 Answers 2

1

scipy.sparse can only handle 2d arrays. You might want to look into pydata/sparse which looks to handle n-dimensional sparse data while following the array interface. At the moment, it has fewer types of arrays and will have some performance issues, but is being actively developed.

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

Comments

0
from scipy.sparse import csr_matrix

data = [1,2,3,4,5,6] 
col = [0,0,0,1,1,1] 
row = [0,1,2,0,1,2] 
a = csr_matrix((data, (row, col)))
a.reshape((3,2))

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.