4
我正在使用python/numpy。作爲輸入數據,我有大量的值對(x,y)
。我基本上想繪製<y>(x)
,即某個數據倉x
的平均值爲y
。目前我使用普通的for
循環來實現這一點,這非常緩慢。平均值取決於binning與第二個變量
# 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]) ])
是否有可能有一種矢量化的文字呢?
哇 - 我沒想到解釋'y'爲 「權重」 的到'x' ...好極了! – 2013-03-18 13:48:15
@JakobS。沒有人會......直到看到它第一次完成! – Jaime 2013-03-18 13:53:37
這確實很酷。 – HyperCube 2013-03-18 13:57:29