[Numpy-discussion] changed behavior of numpy.histogram

Mark.Miller mpmusu@cc.usu....
Wed Jan 23 11:07:18 CST 2008


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?

Thanks,

-Mark



More information about the Numpy-discussion mailing list