numpy:矩阵中每一行的不同子集上的平均值



给定以下矩阵

In [1]: a
Out[1]: 
array([[106.74    , 108.072   , 108.72    , 109.584   , 108.468   ],
[114.012   , 114.156   , 107.928   , 113.904   , 112.968   ],
[114.396225, 115.21124 , 116.01796 , 115.0901  , 113.01843 ]],
dtype=float32)

我可以像这样计算行的每个子集的平均值,

In [2]: np.mean(a[:,1:3],axis=1)
Out[2]: array([108.395996, 111.042   , 115.6146  ], dtype=float32)

但我现在想做的是,

给定一组索引i = [3,4,3]

我想要的方法

a[0,1:3]
a[1,1:4]
a[3,1:2]

所以,我的第一次尝试自然是

In [2]: np.mean(a[:,1:i],axis=1)
TypeError: slice indices must be integers or None or have an __index__ method

这当然不起作用。

我也知道,

In [3]: a[np.where(i)+(i,)]
Out[3]: array([108.468 , 113.904 , 115.0901], dtype=float32)

但我没能找到如何将它们结合起来。。。。

有什么想法吗?

在@luciole75w和@Friedrich回答后更新,

我比较了两个版本的运行时间,

print(a.shape,ii.shape)
#
t = time.time()
mu1b=np.array([np.mean(a[j,0:i]) for j,i in enumerate(ii)])
print(time.time() - t)
# 
t = time.time()
col = np.arange(a.shape[1]).reshape(-1, 1)
istart = 0         # or variable, e.g. istart = [1, 1, 1]
istop = ii  # or fixed, e.g. istop = 3
tmask = (istart <= col) & (col < istop)
mu1 = np.nansum(a.T*tmask,axis=0)/tmask.sum(axis=0)
print(time.time() - t)

结果,

(740832, 30) (740832,)
7.258646249771118
0.5934605598449707

@基于@AndrasDeak建议的luciole75w版本效率更高。

Andras的好主意,我也会使用相同的方法。这个面具可以用类似的东西制作:

istart = 1         # or variable, e.g. istart = [1, 2, 1]
istop = [3, 4, 3]  # or fixed, e.g. istop = 3
col = np.arange(a.shape[1]).reshape(-1, 1)
tmask = (istart <= col) & (col < istop)
#array([[False, False, False],
#       [ True,  True,  True],
#       [ True,  True,  True],
#       [False,  True, False],
#       [False, False, False]], dtype=bool)
np.where(tmask, a.T, 0).sum(axis=0) / tmask.sum(axis=0)

array([ 108.396 ,  111.996 ,  115.6146])

请注意,掩码是转置的,因此可以使用本机python类型轻松地编写输入边界(因为它们是沿轴0应用的(。

您可以使用生成器表达式

a = np.array([[106.74    , 108.072   , 108.72    , 109.584   , 108.468   ],
[114.012   , 114.156   , 107.928   , 113.904   , 112.968   ],
[114.396225, 115.21124 , 116.01796 , 115.0901  , 113.01843 ]])
[np.mean(a[j,1:i]) for j,i in zip([0,1,2],[3,4,2])]

返回:[108.396, 111.996, 114.156]

数组用于处理相同长度的行和列。但是对于结束索引,您需要不同长度的行。

最新更新