我正在使用 python / numpy。作为输入数据,我有大量的值对(x,y)
。我基本上想绘制,即某个数据仓<y>(x)
的平均值。目前我使用一个普通的循环来实现这一点,这非常慢。y
x
for
# 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] ) ])
是否有可能为它提供一种矢量化的写作?