Efficient way to use the Logarithm function in a sparse matrix

I have a large sparse matrix. I want to take log4 for all elements in this sparse matrix.

I am trying to use numpy.log() , but it does not work with matrices.

I can also log line by line. Then I crush the old line with the new one.

 # Assume A is a sparse matrix (Linked List Format) with float values as data # It is only for one row import numpy as np c = np.log(A.getrow(0)) / numpy.log(4) A[0, :] = c 

It was not as fast as I expected. Is there a faster way to do this?

+4
source share
2 answers

You can directly change the data attribute:

 >>> a = np.array([[5,0,0,0,0,0,0],[0,0,0,0,2,0,0]]) >>> coo = coo_matrix(a) >>> coo.data array([5, 2]) >>> coo.data = np.log(coo.data) >>> coo.data array([ 1.60943791, 0.69314718]) >>> coo.todense() matrix([[ 1.60943791, 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0.69314718, 0. , 0. ]]) 

Note that this does not work properly if the sparse format has duplicate elements (which is valid in COO format); he will take the logs separately and log(a) + log(b) != log(a + b) . You probably want to convert to CSR or CSC first (which is fast) to avoid this problem.

You will also have to add checks if the sparse matrix is ​​in a different format, of course. And if you do not want to change the matrix in place, just create a new sparse matrix in the same way as in your answer, but without adding 3 , because this is completely optional here.

+2
source

I think I solve it very easily. It is very strange that no one could answer immediately.

 # Let A be a COO_matrix import numpy as np from scipy.sparse import coo_matrix new_data = np.log(A.data+3)/np.log(4) #3 is not so important. It can be 1 too. A = coo_matrix((new_data, (A.row, A.col)), shape=A.shape) 
0
source

Source: https://habr.com/ru/post/1402979/


All Articles