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

Nathaniel Smith njs at pobox.com
Mon May 21 18:43:13 EDT 2012


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.

- N



More information about the SciPy-Dev mailing list