osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

python numpy histogram


Machiel Kolstein wrote:

> 
> Hi,
> 
> I get the following error:
> 
> ERROR:
> Traceback (most recent call last):
>   File "exponential_distr.py", line 32, in <module>
>     numpy.histogram(data_array, bins=100, range=20000)
>   File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line
>   499, in histogram
>     mn, mx = [mi + 0.0 for mi in range]
> TypeError: 'int' object is not iterable
> 
> with the code shown below.
> I guess I am using "numpy.histogram" below. Also, is there a way to fill
> the histogram on an event by event base without using the clumsy way of
> constructing an array as I do below? >
 
> CODE:
> 
> import numpy
> import numpy.random
> 
> rate = float(1e5)
> average_dt = 1.0/rate
> print "average dt: ", average_dt
> 
> calc_average = 0.0
> total_num=1000.0
> isFirst = True
> for index in range(0, int(total_num)):
>     time = numpy.random.exponential(average_dt)
>     #    print "time: ", time, " = ", time*1e9, " ns"
>     calc_average = calc_average + time
>     
>     # Idiot python way of constructing an array (God, I hate python...)

Numpy is not python, its learning curve is a bit steeper.

>     if (isFirst == True):
>         data_array = time
>         isFirst = False
>     else:
>         data_array = numpy.hstack((data_array, time))

With both, it helps if you read the documentation, or at least the 
docstring:

>>> import numpy
>>> help(numpy.random.exponential)
Help on built-in function exponential:

exponential(...)
    exponential(scale=1.0, size=None)
    
    Exponential distribution.
    
...
    Parameters
    ----------
    ...
    size : tuple of ints
        Number of samples to draw.  The output is shaped
        according to `size`.

So:

data_array = numpy.random.exponential(average_dt, total_num)
calc_average = data_array.mean()

(If you follow the docstring you'd write (total_num,).)

> calc_average = calc_average/total_num
> print "calculated average: ", calc_average, " = ", calc_average*1e9, " ns"
> print "data_array: ", data_array
> 
> numpy.histogram(data_array, bins=100, range=20000)

>>> help(numpy.histogram)
Help on function histogram in module numpy.lib.function_base:

histogram(a, bins=10, range=None, normed=False, weights=None, density=None)
    Compute the histogram of a set of data.
    
    Parameters
    ----------
    ...
    range : (float, float), optional
        The lower and upper range of the bins.  If not provided, range
        is simply ``(a.min(), a.max())``.  Values outside the range are
        ignored.
    ...

> #import matplotlib.pyplot as plt
> #plt.hist(data_array, bins=100, range=20000)
> #plt.show()

> And - final question - how can I most
> easily plot the histogram without having to define - again - the bins
> etc...?

Matplotlib is the way do go for plotting with Python. If matplotlib can do 
what you want you could omit numpy.histogram()...