How to apply a natural logarithm to a matrix and obtain zero for when the matrix entry is zero
问题 In Python I have a Matrix with some zero values, how can I apply a natural logarithm and obtain zero for when the matrix entry is zero? I am using numpy.log(matrix) to apply the natural logarithm function, but I am getting nan when the matrix entry is equal to zero, and I would like it to be zero instead 回答1: You can do something like this: arr = numpy.nan_to_num(numpy.log(matrix)) The behavior of nan_to_num replaces all the NaNs by zeroes. You can find more information here: https://docs