Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
argriffing opened this issue Oct 28, 2013 · 3 comments
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.stats
Milestone

Comments

@argriffing
Copy link
Contributor

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

Fixed by #3041

andrenarchy pushed a commit to andrenarchy/scipy that referenced this issue Dec 4, 2013
stats.entropy will fail when calculate relative entropy of arrays have
ndim > 1. (issue scipy#3030)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.stats
Projects
None yet
Development

No branches or pull requests

3 participants