numpy binned mean,保留额外的轴

时间:2017-09-28 11:20:09

标签: arrays numpy multidimensional-array binning

我似乎对@Bean(name = "A name")的问题感到困惑。

我有一个形状为numpy的数组X 我需要根据X.shape = (nexp, ntime, ndim, npart)(和一些npart)中的值计算此binvals维度上此阵列的分箱统计信息,但保留所有其他维度,因为我必须使用binned统计信息可以删除原始数组bins中的某些偏差。分箱值的形状为X

一个完整的,最小的例子,来解释我想要做的事情。请注意,实际上,我正在研究大型阵列和几个箱子(所以这个实现需要永远):

binvals.shape = (nexp, ntime, npart)

查看结果可能会更清楚吗?

import numpy as np

np.random.seed(12345)

X = np.random.randn(24).reshape(1,2,3,4)
binvals = np.random.randn(8).reshape(1,2,4)
bins = [-np.inf, 0, np.inf]
nexp, ntime, ndim, npart = X.shape

cleanX = np.zeros_like(X)
for ne in range(nexp):
    for nt in range(ntime):
        indices = np.digitize(binvals[ne, nt, :], bins)
        for nd in range(ndim):
            for nb in range(1, len(bins)):
                inds = indices==nb
                cleanX[ne, nt, nd, inds] = X[ne, nt, nd, inds] - \
                     np.mean(X[ne, nt, nd, inds], axis = -1)

有矢量化解决方案吗?我想过使用In [8]: X Out[8]: array([[[[-0.20470766, 0.47894334, -0.51943872, -0.5557303 ], [ 1.96578057, 1.39340583, 0.09290788, 0.28174615], [ 0.76902257, 1.24643474, 1.00718936, -1.29622111]], [[ 0.27499163, 0.22891288, 1.35291684, 0.88642934], [-2.00163731, -0.37184254, 1.66902531, -0.43856974], [-0.53974145, 0.47698501, 3.24894392, -1.02122752]]]]) In [10]: cleanX Out[10]: array([[[[ 0. , 0.67768523, -0.32069682, -0.35698841], [ 0. , 0.80405255, -0.49644541, -0.30760713], [ 0. , 0.92730041, 0.68805503, -1.61535544]], [[ 0.02303938, -0.02303938, 0.23324375, -0.23324375], [-0.81489739, 0.81489739, 1.05379752, -1.05379752], [-0.50836323, 0.50836323, 2.13508572, -2.13508572]]]]) In [12]: binvals Out[12]: array([[[ -5.77087303e-01, 1.24121276e-01, 3.02613562e-01, 5.23772068e-01], [ 9.40277775e-04, 1.34380979e+00, -7.13543985e-01, -8.31153539e-01]]]) ,但我似乎无法理解如何将它用于此目标。谢谢!

2 个答案:

答案 0 :(得分:2)

import numpy as np

np.random.seed(100)

nexp = 3
ntime = 4
ndim = 5
npart = 100
nbins = 4

binvals = np.random.rand(nexp, ntime, npart)
X = np.random.rand(nexp, ntime, ndim, npart)
bins = np.linspace(0, 1, nbins + 1)

d = np.digitize(binvals, bins)[:, :, np.newaxis, :]
r = np.arange(1, len(bins)).reshape((-1, 1, 1, 1, 1))
m = d[np.newaxis, ...] == r
counts = np.sum(m, axis=-1, keepdims=True).clip(min=1)
means = np.sum(X[np.newaxis, ...] * m, axis=-1, keepdims=True) / counts
cleanX = X - np.choose(d - 1, means)

答案 1 :(得分:1)

好的,我认为我得到了它,主要是基于@jdehesa的答案。

clean2 = np.zeros_like(X)
d = np.digitize(binvals, bins)
for i in range(1, len(bins)):
    m = d == i
    minds = np.where(m)
    sl = [*minds[:2], slice(None), minds[2]]
    msum = m.sum(axis=-1)
    clean2[sl] = (X - \
                  (np.sum(X * m[...,np.newaxis,:], axis=-1) / 
                  msum[..., np.newaxis])[..., np.newaxis])[sl]

其结果与原始代码相同。 在这里的例子中,这个解决方案的速度大约是原始代码的三倍。我希望它在更大的阵列上更快。

更新

实际上它在较大的阵列上更快(没有进行任何正式测试),但尽管如此,它在性能方面达到了可接受的水平......任何关于额外矢量化的进一步建议都将非常受欢迎。