按索引列表拆分NumPy数组

kqqjbcuj  于 2023-08-05  发布在  其他
关注(0)|答案(2)|浏览(117)

我试着得到数据的每个子组的平均值,按一系列指数分组。我可能没有正确地解释它,但下面的示例代码应该清楚地指出我想做什么。

import numpy as np
means = [data[i].mean(axis=0) for i in indices]

# data.shape = (17 492, 3 650)
# data.dtype = float64
# indices.shape = (30, 17 492)
# indices.dtype = bool

字符串
这里,data是一个N * M浮点数组,indices是一个L * N布尔掩码数组。索引的每个L是指示要一起平均的第l组数据的布尔掩码。
根据数据大小,这是我的过程中最密集的部分。如果有一种方法可以完全使用NumPy函数来实现这一点,我想重构它。

4sup72z8

4sup72z81#

更小的样本:

In [261]: data = np.arange(100).reshape(20,5)
In [262]: indices =  np.random.rand(10,20)>.5

字符串
您的列表理解:

In [263]: means = [data[i].mean(axis=0) for i in indices]
In [264]: np.array(means)
Out[264]: 
array([[24.        , 25.        , 26.        , 27.        , 28.        ],
       [50.45454545, 51.45454545, 52.45454545, 53.45454545, 54.45454545],
       [35.55555556, 36.55555556, 37.55555556, 38.55555556, 39.55555556],
       [50.625     , 51.625     , 52.625     , 53.625     , 54.625     ],
       [60.        , 61.        , 62.        , 63.        , 64.        ],
       [53.33333333, 54.33333333, 55.33333333, 56.33333333, 57.33333333],
       [41.875     , 42.875     , 43.875     , 44.875     , 45.875     ],
       [59.375     , 60.375     , 61.375     , 62.375     , 63.375     ],
       [33.125     , 34.125     , 35.125     , 36.125     , 37.125     ],
       [54.5       , 55.5       , 56.5       , 57.5       , 58.5       ]])


整个数组方法,创建一个更大的数组,其中nan用于屏蔽的值。有一整套np.nan...函数可以从浮点数组中剥离nan,并在没有它们的情况下进行计算。

In [265]: res = np.zeros((10,20,5))
In [266]: res[:] = data
In [267]: res[~indices]=np.nan
In [268]: mean2 =np.nanmean(res, axis=1)
In [269]: mean2
Out[269]: 
array([[24.        , 25.        , 26.        , 27.        , 28.        ],
       [50.45454545, 51.45454545, 52.45454545, 53.45454545, 54.45454545],
       [35.55555556, 36.55555556, 37.55555556, 38.55555556, 39.55555556],
       [50.625     , 51.625     , 52.625     , 53.625     , 54.625     ],
       [60.        , 61.        , 62.        , 63.        , 64.        ],
       [53.33333333, 54.33333333, 55.33333333, 56.33333333, 57.33333333],
       [41.875     , 42.875     , 43.875     , 44.875     , 45.875     ],
       [59.375     , 60.375     , 61.375     , 62.375     , 63.375     ],
       [33.125     , 34.125     , 35.125     , 36.125     , 37.125     ],
       [54.5       , 55.5       , 56.5       , 57.5       , 58.5       ]])


值匹配;我不知道时机。我从这个例子中得到的可能不适用于你的更大的情况。
查看nanmean代码,我发现了一种更直接的方法:

In [286]: cnt = np.sum(indices, axis=1)
In [287]: x = (data*indices[:,:,None]).sum(axis=1)
In [288]: x/cnt[:,None]
Out[288]: 
array([[24.        , 25.        , 26.        , 27.        , 28.        ],
       [50.45454545, 51.45454545, 52.45454545, 53.45454545, 54.45454545],
       [35.55555556, 36.55555556, 37.55555556, 38.55555556, 39.55555556],
       [50.625     , 51.625     , 52.625     , 53.625     , 54.625     ],
       [60.        , 61.        , 62.        , 63.        , 64.        ],
       [53.33333333, 54.33333333, 55.33333333, 56.33333333, 57.33333333],
       [41.875     , 42.875     , 43.875     , 44.875     , 45.875     ],
       [59.375     , 60.375     , 61.375     , 62.375     , 63.375     ],
       [33.125     , 34.125     , 35.125     , 36.125     , 37.125     ],
       [54.5       , 55.5       , 56.5       , 57.5       , 58.5       ]])

zsbz8rwp

zsbz8rwp2#

我想你想这么做?

import numpy as np

# Create a masked arraybased on the data and indices
masked_data = np.ma.masked_array(data, ~indices)

means = np.mean(masked_data, axis=0)

字符串

相关问题