Search code examples
pythonnumpyscipysparse-matrixlogarithm

taking the log of every element in a sparse matrix


How can you take the log base 10 of every element in a sparse matrix (COO)?

>>print type(X)
<class 'scipy.sparse.coo.coo_matrix'>

I've tried this but it doesn't work:

import math
X.data = math.log(X.data,10)

Solution

  • Use np.log10 rather than math.log10:

    import numpy as np
    X.data = np.log10(X.data)