python - 根据另一个数组的索引取一个数组的平均值

标签 python arrays numpy mean

假设我有一个如下所示的数组:

a = np.array([0, 20, 40, 30, 60, 35, 15, 18, 2])

我有一个索引数组,我想在它们之间求平均值:

averaging_indices = np.array([2, 4, 7, 8])

我想要做的是根据averaging_indices数组对数组a的元素进行平均。为了清楚起见,我想取平均值:

np.mean(a[0:2]), np.mean(a[2:4]), np.mean(a[4:7]), np.mean(a[7,8]), np.mean(a[8:])

我想返回一个具有正确尺寸的数组,在本例中为

result = [10, 35, 36.66, 18, 2]

有人能想出一个巧妙的方法来做到这一点吗?我能想到的唯一方法是循环,这是非常反numpy的。

最佳答案

这是一种矢量化方法 np.bincount -

# Create "shifts array" and then IDs array for use with np.bincount later on
shifts_array = np.zeros(a.size,dtype=int)
shifts_array[averaging_indices] = 1
IDs = shifts_array.cumsum()

# Use np.bincount to get the summations for each tag and also tag counts.
# Thus, get tagged averages as final output.
out = np.bincount(IDs,a)/np.bincount(IDs)

示例输入、输出 -

In [60]: a
Out[60]: array([ 0, 20, 40, 30, 60, 35, 15, 18,  2])

In [61]: averaging_indices
Out[61]: array([2, 4, 7, 8])

In [62]: out
Out[62]: array([ 10.        ,  35.        ,  36.66666667,  18.        ,   2.        ])

关于python - 根据另一个数组的索引取一个数组的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35411879/

相关文章:

java - 如何将 ArrayList 内的对象中的值设为 "edit"?

python - 使用Python读取文件夹中的wav文件

python - 将 JavaScript 变量提取到 Python 字典中

python - 如何迭代以下方程以确定根

php - 使用 PHP 的准备语句将数组插入 MySQL 数据库

c - 使用 IN/OUT 标志每行显示一个字符串

python - 替换 numpy ndarray 中的字符(Python)

python - 当多维形状是列表时,Numba 函数中的 NumPy 零不起作用

Python二叉树

python - 在pygame中使用滚动相机