python numpy histogram

2019-06-18 Thread Machiel Kolstein

Hi, 

I get the following error: 

ERROR: 
Traceback (most recent call last):
  File "exponential_distr.py", line 32, in 
numpy.histogram(data_array, bins=100, range=2)
  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?
And - final question - how can I most easily plot the histogram without having 
to define - again - the bins etc...?

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...)
if (isFirst == True): 
data_array = time
isFirst = False
else:
data_array = numpy.hstack((data_array, time))

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=2)
#   import matplotlib.pyplot as plt
#   plt.hist(data_array, bins=100, range=2)
#   plt.show()




-- 
Avís -
Aviso - Legal Notice - (LOPD) - http://legal.ifae.es 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python numpy histogram

2019-06-18 Thread Machiel Kolstein


Thank you for your fast reply. You're right, I completely missed the fact that 
range should be given with two numbers (which in retrospect should have been 
obvious). 
Best regards, 

Machiel

-- 
Avís -
Aviso - Legal Notice - (LOPD) - http://legal.ifae.es 

-- 
https://mail.python.org/mailman/listinfo/python-list


Fit to function values with numpy/scipy

2019-11-25 Thread Machiel Kolstein

If I have an array with values, which are distributed according to a Gaussian 
function, then I can fit with: 
   (fit_mu, fit_sigma) = stats.norm.fit(x_array)

However, now, I have one array with values for the xbins (e.g., 0.0, 0.1, 0.2, 
0.3, ..., up till 1.0) and one value for the corresponding y-value (e.g. 0.0, 
0.3, 0.6, 1.2, 5.0, 10.0, 5.0, 1.2, 0.6, 0.3, 0.0).
(These values are just an example). 
Now I want to fit this, with a Gauss. So, obviously I don't want to fit over 
neither the values in xbins, nor the y_array (neither of which is normal 
distributed) but over the y values for each x bin. 
The only thing I can think of is looping over all bins, and then filling an 
artificial array: 

for i in range(0, Nbins): 
   x = xbinvalue(i)
   weight = y_value_for_this_x(x)
   for w in range(0, weight)
   numpy.vstack((tmp_array, x)
(fit_mu, fit_sigma) = scipy.stats.norm.fit(tmp_array)

But this seems a rather silly way of doing this. Is there an other way?

Cheers, 

Machiel   

-- 
Avís -
Aviso - Legal Notice - (LOPD) - http://legal.ifae.es 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fit to function values with numpy/scipy

2019-11-25 Thread Machiel Kolstein

Okay, I found some answer myself: use scipy.optimize.curve_fit
However, I still find it strange that I have to define a gauss function myself 
instead of it being readily available. I did this: 

# Define model function to be used to fit to the data
def gauss(x, *p):
A, mu, sigma = p
return A*np.exp(-(x-mu)**2/(2.*sigma**2))
p0 = [1., 0., 1.]

# Fit the histogram -
coeff, var_matrix = curve_fit(gauss, x_array, y_array, p0=p0)
amplitude, mu, sigma = coeff
print "amplitude, mu, sigma = ", amplitude, mu, sigma

# Get the fitted curve
hist_fit = gauss(x_array, *coeff)
plt.plot(x_array, hist_fit, color='red', linewidth=5, label='Fitted data')

plt.show()

-- 
Avís -
Aviso - Legal Notice - (LOPD) - http://legal.ifae.es 

-- 
https://mail.python.org/mailman/listinfo/python-list