python - 高效使用 numpy_indexed 输出

标签 python numpy

>>> import numpy_indexed as npi
>>> import numpy as np
>>> a = np.array([[0,0,1,1,2,2], [4,4,8,8,10,10]]).T
>>> a
array([[ 0,  4],
       [ 0,  4],
       [ 1,  8],
       [ 1,  8],
       [ 2, 10],
       [ 2, 10]])
>>> npi.group_by(a[:, 0]).sum(a[:,1])

(array([0, 1, 2]), array([ 8, 16, 20], dtype=int32))

我想在大型集合(~1m 行)上对由第一列聚集的第二列的子集执行计算。是否有一种有效(和/或向量化)的方式来使用 numpy_indexedgroup_by 输出,以便添加一个包含这些计算输出的新列?在上面的 sum 示例中,我想生成以下输出。

如果有一种无需首先使用 numpy_indexed 即可执行此操作的有效方法,那也会非常有帮助。

array([[ 0,  4,  8],
       [ 0,  4,  8],
       [ 1,  8, 16],
       [ 1,  8, 16],
       [ 2, 10, 20],
       [ 2, 10, 20]])

最佳答案

一种方法 np.unique生成那些独特的标签和间隔移位索引,然后 np.add.reduceat对于 intervaled-summing -

_,idx,tags = np.unique(a[:,0], return_index=1, return_inverse=1)
out = np.c_[a, np.add.reduceat(a[:,1],idx)[tags]]

另一种避免使用 np.unique 并可能对性能有益的方法是这样的 -

idx = np.r_[0,np.flatnonzero(a[1:,0] > a[:-1,0])+1]
tag_arr = np.zeros(a.shape[0], dtype=int)
tag_arr[idx[1:]] = 1
tags = tag_arr.cumsum()
out = np.c_[a, np.add.reduceat(a[:,1],idx)[tags]]

为了进一步提升性能,我们应该使用np.bincount。因此,np.add.reduceat(a[:,1],idx) 可以替换为 np.bincount(tags, a[:,1])

sample 运行-

In [271]: a    # Using a more generic sample
Out[271]: 
array([[11,  4],
       [11,  4],
       [14,  8],
       [14,  8],
       [16, 10],
       [16, 10]])

In [272]: _,idx,tags = np.unique(a[:,0], return_index=1, return_inverse=1)

In [273]: np.c_[a, np.add.reduceat(a[:,1],idx)[tags]]
Out[273]: 
array([[11,  4,  8],
       [11,  4,  8],
       [14,  8, 16],
       [14,  8, 16],
       [16, 10, 20],
       [16, 10, 20]])]

现在,列出的方法假设第一列已经排序。如果不是这种情况,我们需要按第一列 argsort 对数组进行排序,然后使用建议的方法。因此,对于未排序的情况,我们需要以下内容作为预处理 -

a = a[a[:,0].argsort()]

对抗 np.unique

让我们根据内置的 np.unique 对基于自定义 flatnonzero + cumsum 的方法进行计时,以创建移动索引: idx 和基于唯一性的 ID/标签:tags。对于这种情况,我们事先知道标签列已经排序,我们避免任何排序,就像使用 np.unique 所做的那样。这使我们在性能上具有优势。那么,让我们来验证一下吧。

方法-

def nonzero_cumsum_based(A):
    idx = np.concatenate(( [0] ,np.flatnonzero(A[1:] > A[:-1])+1 ))
    tags = np.zeros(len(A), dtype=int)
    tags[idx[1:]] = 1
    np.cumsum(tags, out = tags)
    return idx, tags

def unique_based(A):
    _,idx,tags = np.unique(A, return_index=1, return_inverse=1)
    return idx, tags

使用自定义函数运行示例 -

In [438]: a
Out[438]: 
array([[11,  4],
       [11,  4],
       [14,  8],
       [14,  8],
       [16, 10],
       [16, 10]])

In [439]: idx, tags = nonzero_cumsum_based(a[:,0])

In [440]: idx
Out[440]: array([0, 2, 4])

In [441]: tags
Out[441]: array([0, 0, 1, 1, 2, 2])

时间 -

In [444]: a = np.c_[np.sort(randi(10,10000,(100000))), randi(0,10000,(100000))]

In [445]: %timeit unique_based(a[:,0])
100 loops, best of 3: 4.3 ms per loop

In [446]: %timeit nonzero_cumsum_based(a[:,0])
1000 loops, best of 3: 486 µs per loop

In [447]: a = np.c_[np.sort(randi(10,10000,(1000000))), randi(0,10000,(1000000))]

In [448]: %timeit unique_based(a[:,0])
10 loops, best of 3: 50.2 ms per loop

In [449]: %timeit nonzero_cumsum_based(a[:,0])
100 loops, best of 3: 3.98 ms per loop

关于python - 高效使用 numpy_indexed 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44045066/

相关文章:

python - 在调用 Django 模型保存/删除后调用函数

python - TensorFlow 如何初始化全局步

python - 将numpy uint8输入tensorflow float32占位符

python-3.x - 使用随机整数和跨列的有限总和创建 pandas 数据框

python - 如何在 pythran/numpy 中查看强制转换/重新解释强制转换?

python - 黑色格式化程序 - 忽略特定的多行代码

python - 如何使用 REST API 从 Python 脚本更新 Jira v5.1 中的问题