[SciPy-Dev] warnings in scipy.stats.entropy

josef.pktd at gmail.com josef.pktd at gmail.com
Mon May 21 19:11:14 EDT 2012


On Mon, May 21, 2012 at 6:43 PM, Nathaniel Smith <njs at pobox.com> wrote:
> On Mon, May 21, 2012 at 11:39 PM, Skipper Seabold <jsseabold at gmail.com> wrote:
>> Currently in scipy.stats.entropy if you are not ignoring them you will
>> see warnings when the function is given a probability of zero even
>> though the case of zero is specifically handled in the function.
>> Rightly or wrongly this makes me cringe. What do people think about
>> fixing this by using seterr explicitly in the function or masking the
>> zeros. Eg.,
>>
>> import numpy as np
>> from scipy.stats import entropy
>>
>> prob = np.random.uniform(0,20, size=10)
>> prob[5] = 0
>> prob = prob/prob.sum()
>>
>> np.seterr(all = 'warn')
>> entropy(prob) # too loud
>>
>> Instead we could do (within entropy)
>>
>> oldstate = np.geterr()
>> np.seterr(divide='ignore', invalid='ignore')
>> entropy(prob)
>> np.seterr(**oldstate)
>>
>> or just mask the zeros in the first place if this is too much
>>
>> idx = prob > 0
>> -np.sum(prob[idx] * np.log(prob[idx]))
>>
>> Thoughts?
>
> I like the mask version better.

+1,

buggy: if qk is given, then the function isn't vectorized.

Josef

>
> - N
> _______________________________________________
> SciPy-Dev mailing list
> SciPy-Dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-dev



More information about the SciPy-Dev mailing list