LoginSignup
2
3

More than 5 years have passed since last update.

scipy.sparseの非ゼロ要素の対数を取る

Posted at

調べてもあまり出てこなかったのでメモ。

参考:http://stackoverflow.com/questions/6256206/scipy-sparse-default-value

ポイントはlilだと出来ないけどcoo, csr, cscならできる所。

>>> import numpy as np
>>> from scipy.sparse import lil_matrix
>>> a = lil_matrix((3,2))
>>> a[0,0] = 10
>>> a[0,1] = 3
>>> a[1,0] = 11
>>> a[1,1] = 100
>>> a[2,0] = 12
>>> a.todense()
matrix([[  10.,    3.],
        [  11.,  100.],
        [  12.,    0.]])
>>> b = a.tocsr()
>>> a.data
array([[10.0, 3.0], [11.0, 100.0], [12.0]], dtype=object)
>>> np.log(a.data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'log'
>>> b.data
array([  10.,    3.,   11.,  100.,   12.])
>>> np.log(b.data)
array([ 2.30258509,  1.09861229,  2.39789527,  4.60517019,  2.48490665])
>>> b.data = np.log(b.data)
>>> b.todense()
matrix([[ 2.30258509,  1.09861229],
        [ 2.39789527,  4.60517019],
        [ 2.48490665,  0.        ]])
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3