I have a sparse matrix in which I want to increment all the values of non-zero elements by one. However, I cannot figure it out. Is there a way to do it using standard packages in python? Any help will be appreciated.
Asked
Active
Viewed 5,530 times
2
-
How many dimensions you matrix have? – Hemerson Tacon Sep 29 '18 at 03:41
-
Hi, I am using a 1Mx1.5M dimensional matrix. I tried using a numpy matrix, but it works on smaller dimensions. This is too big for my system, I am using 16GB of RAM right now. So I just wanted to add values to all the non zero elements in a sparse matrix. – Saugata Paul Oct 05 '18 at 04:02
-
Have you tried the solution that I wrote in my answer below? – Hemerson Tacon Oct 05 '18 at 11:51
4 Answers
4
I cannot comment on it's performance but you can do (Scipy 1.1.0);
>>> from scipy.sparse import csr_matrix
>>> a = csr_matrix([[0, 2, 0], [1, 0, 0]])
>>> print(a)
(0, 1) 2
(1, 0) 1
>>> a[a.nonzero()] = a[a.nonzero()] + 1
>>> print(a)
(0, 1) 3
(1, 0) 2

OldWolfs
- 606
- 6
- 15
-
This gives error- ```NotImplementedError: adding a nonzero scalar to a sparse matrix is not supported``` – atinjanki May 13 '20 at 01:15
-
@atinjanki Hi, thanks for the feedback. It may related to scipy version, I've updated my response. – OldWolfs May 14 '20 at 15:18
1
If your matrix have 2 dimensions, you can do the following:
sparse_matrix = [[element if element==0 else element+1 for element in row ]for row in sparse_matrix]
It will iterate over every element of your matrix and return the element without any change if it is equals to zero, else it add 1 to the element and return it.
More about conditionals in list comprehension in the answer for this question.

Hemerson Tacon
- 2,419
- 1
- 16
- 28
1
You can use the package numpy
which has efficient functions for dealing with n-dimensional arrays. What you need is:
array[array>0] += 1
where array
is the numpy array of your matrix. Example here:
`
import numpy as np
my_matrix = [[2,0,0,0,7],[0,0,0,4,0]]
array = np.array(my_matrix);
print("Matrix before incrementing values: \n", array)
array[array>0] += 1
print("Matrix after incrementing values: \n", array)`
Outputs:
Matrix before incrementing values:
[[2 0 0 0 7]
[0 0 0 4 0]]
Matrix after incrementing values:
[[3 0 0 0 8]
[0 0 0 5 0]]
Hope this helps!

crow3487
- 135
- 1
- 10
-
Hi, thanks for the reply! I have tried this approach before & it works well for matrices having a reasonable dimension. In my case I am am using a [1Mx1.5M] dimensional matrix. M is a million here. So I am not being able to convert this sparse matrix to a numpy array as it is giving me a memory error. I am having 16 GB of RAM, and there's no way I can remove the features or delete some rows. So the ideal solution will be a sparse matrix. So as a correction to my question, I wanted to ask specifically about adding values to non zero elements in a sparse matrix and not a numpy array. – Saugata Paul Oct 05 '18 at 03:59
1
When you have a scipy
sparse matrix (scipy.sparse
) is:
import scipy.sparse as sp
my_matrix = [[2,0,0,0,7],[0,0,0,4,0]]
my_matrix = sp.csc_matrix(my_matrix)
my_matrix.data += 1
my_matrix.todense()
Returns:
[[3, 0, 0, 0, 8], [0, 0, 0, 5, 0]]

dnc423
- 11
- 1