[Numpy-discussion] changed behavior of numpy.histogram

Travis E. Oliphant oliphant at enthought.com
Wed Jan 23 12:15:15 EST 2008


Mark.Miller wrote:
> Greetings:  I just noticed a changed behavior of numpy.histogram.  I 
> think that a recent 'fix' to the code has changed my ability to use that 
> function (albeit in an unconventional manner).  I previously used the 
> histogram function to obtain counts of each unique string within a 
> string array.  Again, I recognize that it is not a typical use of the 
> histogram function, but it did work very nicely for me.
>
> Here's an example:
>
> ###numpy 1.0.3  --works just fine
>  >>> import numpy
>  >>> numpy.__version__
> '1.0.3'
>  >>> a=numpy.array(('atcg', 'atcg', 'aaaa', 'aaaa'))
>  >>> a
> array(['atcg', 'atcg', 'aaaa', 'aaaa'],
>        dtype='|S4')
>  >>> b=numpy.unique(a)
>  >>> numpy.histogram(a,b)
> (array([2, 2]), array(['aaaa', 'atcg'],
>        dtype='|S4'))
>  >>>
>
> ###numpy 1.0.4  --no longer functions
>  >>> import numpy
>  >>> numpy.__version__
> '1.0.4'
>  >>> a=numpy.array(('atcg', 'atcg', 'aaaa', 'aaaa'))
>  >>> a
> array(['atcg', 'atcg', 'aaaa', 'aaaa'],
>        dtype='|S4')
>  >>> b=numpy.unique(a)
>  >>> numpy.histogram(a,b)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>    File 
> "/opt/libraries/python/python-2.5.1/numpy-1.0.4-gnu/lib/python2.5/site-packages/numpy/lib/function_base.py", 
> line 154, in histogram
>      if(any(bins[1:]-bins[:-1] < 0)):
> TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 
> 'numpy.ndarray'
>  >>>
>
> Is this something that can possibly be fixed (should I submit a ticket)? 
>   Or should I revert to some other approaches for implementing the same 
> idea?  It really was a nice convenience.  Or, alternately, would some 
> sort of new function along the lines of a numpy.countunique() ultimately 
> be useful?
>   
It can be and should be fixed.    Interesting.  I'll have to look back 
at the changes to see what was done before.   The problem is the string 
array does not support the monoticity check (it is also unnecessary for 
that use case, I believe).


-Travis




More information about the NumPy-Discussion mailing list