有效使用numpy_indexed输出

时间:2017-05-18 10:19:46

标签: python numpy numpy-indexed

>>> 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行)上第一列聚类的第二列子集进行计算。是否有一种有效(和/或矢量化)方式使用group_by numpy_indexed的输出来添加带有这些计算输出的新列?在上面的sum示例中,我想生成下面的输出。

如果有一种有效的方法,首先不使用numpy_indexed,这也会非常有帮助。

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

2 个答案:

答案 0 :(得分:3)

使用np.unique生成这些唯一标记和区间转移索引的一种方法,然后intervaled-summing生成np.add.reduceat -

_,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])

示例运行 -

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

让我们花时间针对内置flatnonzero的自定义cumsum + np.unique方法来创建转移索引:idx以及基于唯一性的ID /标记:{ {1}}。对于像这样的情况,我们事先知道标签列已经排序,我们避免任何排序,就像使用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])

答案 1 :(得分:1)

每个索引对象都有一个inverse属性,它将缩小的值映射回原始范围;为了说明,我们可以写:

index = npi.as_index(keys)
unique_keys = index.unique
unique_keys[index.inverse] == keys  # <- should be all true

此属性也在GroupBy对象上公开;因为确实将分组值映射回其输入范围是一项非常有用的操作:

groups = npi.group_by(a[:, 0])
unique, sums = groups.sum(a[:, 1])
new_column = sums[groups.inverse]

一般来说,numpy_indexed的来源可以成为如何执行这种常见操作的灵感来源; group_by.var面临同样的问题,例如,将每个组的均值广播回到它所形成的组的每个元素,以计算每个组中的错误。但是更好的教程肯定也不会受到伤害。

您能否对您要解决的问题进行更高层次的描述?有可能在更高层次上更加简化您的代码,当您在npi方便的设计模式方面思考时更加舒适。

相关问题