numpy: Efficiently avoid 0s when taking log(matrix)

前端 未结 7 644
陌清茗
陌清茗 2020-12-13 18:12
from numpy import *

m = array([[1,0],
           [2,3]])

I would like to compute the element-wise log2(m), but only in the places whe

7条回答
  •  眼角桃花
    2020-12-13 18:33

    We can use masked arrays for this:

    >>> from numpy import *
    >>> m = array([[1,0], [2,3]])
    >>> x = ma.log(m)
    >>> print x.filled(0)
    [[ 0.          0.        ]
     [ 0.69314718  1.09861229]]
    

提交回复
热议问题