2

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.

Saugata Paul
  • 59
  • 1
  • 9

4 Answers4

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