sum 使用索引列表对 numpy 矩阵的行进行分组

muo*_*uon 1 python numpy vectorization

使用索引列表和应用函数切片 numpy 数组,是否可以矢量化(或非矢量化的方式来做到这一点)?向量化将是大型矩阵的理想选择

import numpy as np
index = [[1,3], [2,4,5]]
a = np.array(
       [[ 3,  4,  6,  3],
        [ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15],
        [1, 1,    4,  5]])
Run Code Online (Sandbox Code Playgroud)

按 中的行索引组求和index,给出:

np.array([[8, 10, 12, 14],
          [17, 19, 24, 37]])
Run Code Online (Sandbox Code Playgroud)

Div*_*kar 5

方法#1:这是一种几乎*矢量化的方法-

def sumrowsby_index(a, index):
    index_arr = np.concatenate(index)
    lens = np.array([len(i) for i in index])
    cut_idx = np.concatenate(([0], lens[:-1].cumsum() ))
    return np.add.reduceat(a[index_arr], cut_idx)
Run Code Online (Sandbox Code Playgroud)

*几乎是因为lens使用循环理解计算的步骤,但由于我们只是获取长度并且不涉及计算,因此该步骤不会以任何大的方式影响时间。

样品运行 -

In [716]: a
Out[716]: 
array([[ 3,  4,  6,  3],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [ 1,  1,  4,  5]])

In [717]: index
Out[717]: [[1, 3], [2, 4, 5]]

In [718]: sumrowsby_index(a, index)
Out[718]: 
array([[ 8, 10, 12, 14],
       [17, 19, 24, 27]])
Run Code Online (Sandbox Code Playgroud)

方法#2:我们可以利用快速矩阵乘法numpy.dot来执行这些和约简,为我们提供了另一种方法,如下所示 -

def sumrowsby_index_v2(a, index):
    lens = np.array([len(i) for i in index])
    id_ar = np.zeros((len(lens), a.shape[0]))
    c = np.concatenate(index)
    r = np.repeat(np.arange(len(index)), lens)    
    id_ar[r,c] = 1
    return id_ar.dot(a)
Run Code Online (Sandbox Code Playgroud)