I am working with python / numpy. As input data I have a large number of value pairs (x,y)
. I basically want to plot <y>(x)
, i.e., the mean value of y
for a certain data bin x
. At the moment I use a plain for
loop to achieve this, which is terribly slow.
# create example data
x = numpy.random.rand(1000)
y = numpy.random.rand(1000)
# set resolution
xbins = 100
# find x bins
H, xedges, yedges = numpy.histogram2d(x, y, bins=(xbins,xbins) )
# calculate mean and std of y for each x bin
mean = numpy.zeros(xbins)
std = numpy.zeros(xbins)
for i in numpy.arange(xbins):
mean[i] = numpy.mean(y[ numpy.logical_and( x>=xedges[i], x<xedges[i+1] ) ])
std[i] = numpy.std (y[ numpy.logical_and( x>=xedges[i], x<xedges[i+1] ) ])
Is it possible to have a kind of vectorized writing for it?