Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
argriffing opened this Issue · 3 comments

3 participants

@argriffing
Collaborator

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
Collaborator

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
Collaborator

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

@richardtsai richardtsai referenced this issue from a commit in richardtsai/scipy
@richardtsai richardtsai BUG: stats.entropy broken (issue #3030)
stats.entropy will fail when calculate relative entropy of arrays have
ndim > 1. (issue #3030)
2177b93
@WarrenWeckesser
Collaborator

Fixed by #3041

@andrenarchy andrenarchy referenced this issue from a commit in andrenarchy/scipy
@richardtsai richardtsai BUG: stats.entropy broken (issue #3030)
stats.entropy will fail when calculate relative entropy of arrays have
ndim > 1. (issue #3030)
af03f5f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.