RuntimeWarning: divide by zero encountered in log

前端 未结 5 1146
无人共我
无人共我 2020-12-02 01:25

I am using numpy.log10 to calculate the log of an array of probability values. There are some zeros in the array, and I am trying to get around it using

resu         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 02:13

    Just use the where argument in np.log10

    import numpy as np
    np.random.seed(0)
    
    prob = np.random.randint(5, size=4) /4
    print(prob)
    
    result = np.where(prob > 0.0000000001, prob, -10)
    # print(result)
    np.log10(result, out=result, where=result > 0)
    print(result)
    

    Output

    [1.   0.   0.75 0.75]
    [  0.         -10.          -0.12493874  -0.12493874]
    

提交回复
热议问题