Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

relative entropy using scipy.stats.distribution.entropy when input arrays have ndim > 1 #3030

argriffing opened this Issue Oct 28, 2013 · 3 comments


None yet
3 participants

argriffing commented Oct 28, 2013

This seems to be broken.

$ python
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import scipy.stats.distributions
>>> a = np.random.rand(2, 3)
>>> scipy.stats.distributions.entropy(a)
array([ 0.60673183,  0.27477313,  0.56533111])
>>> scipy.stats.distributions.entropy(a, a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/argriffi/.local/lib/python2.7/site-packages/scipy/stats/distributions.py", line 6130, in entropy
    if any(take(pk, nonzero(qk == 0.0), axis=0) != 0.0, 0):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

josef-pkt commented Oct 28, 2013

Yes, a bug given that the docstring indicates sums are take with axis=0

Also we should get inf only in those positions where the column satisfies the condition.


josef-pkt commented Oct 28, 2013

I think it should be something like

inf_mask = ((qk == 0) & (pk > 0)).any(0)
and if S is not scalar
S[inf_mask] = np.inf

@WarrenWeckesser WarrenWeckesser added a commit that referenced this issue Nov 6, 2013

@WarrenWeckesser WarrenWeckesser Merge pull request #3041 from richardtsai/fix_relative_entropy_ndim
BUG: stats.entropy broken (issue #3030)

WarrenWeckesser commented Nov 6, 2013

Fixed by #3041

@andrenarchy andrenarchy added a commit to andrenarchy/scipy that referenced this issue Dec 4, 2013

@richardtsai @andrenarchy richardtsai + andrenarchy BUG: stats.entropy broken (issue #3030)
stats.entropy will fail when calculate relative entropy of arrays have
ndim > 1. (issue #3030)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment