Numpy通过多个向量进行分组,获得组索引

fykwrbwg  于 11个月前  发布在  其他
关注(0)|答案(3)|浏览(108)

我有几个numpy数组;我想构建一个groupby方法,它将为这些数组提供组ID。然后,它将允许我根据组ID对这些数组进行索引,以对组执行操作。
例如:

import numpy as np
import pandas as pd
a = np.array([1,1,1,2,2,3])
b = np.array([1,2,2,2,3,3])

def group_np(groupcols):
    groupby = np.array([''.join([str(b) for b in bs]) for bs in zip(*[c for c in groupcols])])
    _, groupby = np.unique(groupby, return_invesrse=True)
   return groupby

def group_pd(groupcols):
    df = pd.DataFrame(groupcols[0])
    for i in range(1, len(groupcols)):
        df[i] = groupcols[i]
    for i in range(len(groupcols)):
        df[i] = df[i].fillna(-1)
    return df.groupby(list(range(len(groupcols)))).grouper.group_info[0]

输出:

group_np([a,b]) -> [0, 1, 1, 2, 3, 4]
group_pd([a,b]) -> [0, 1, 1, 2, 3, 4]

有没有更有效的方法来实现它,理想情况下是纯numpy?目前的瓶颈似乎是构建一个对每个组都有唯一值的向量-目前我正在通过将每个向量的值连接为字符串来实现这一点。
我希望这适用于任何数量的输入向量,它可以有数百万个元素。
编辑:这里是另一个测试用例:

a = np.array([1,2,1,1,1,2,3,1])
b = np.array([1,2,2,2,2,3,3,2])

这里,组元素2、3、4、7应该都是相同的。
编辑2:添加一些基准。

a = np.random.randint(1, 1000, 30000000)
b = np.random.randint(1, 1000, 30000000)
c = np.random.randint(1, 1000, 30000000)

def group_np2(groupcols):
    _, groupby = np.unique(np.stack(groupcols), return_inverse=True, axis=1)
    return groupby

%timeit group_np2([a,b,c])
# 25.1 s +/- 1.06 s per loop (mean +/- std. dev. of 7 runs, 1 loop each)
%timeit group_pd([a,b,c])
# 21.7 s +/- 646 ms per loop (mean +/- std. dev. of 7 runs, 1 loop each)
cngwdvgl

cngwdvgl1#

在数组ab上使用np.stack之后,如果在np.unique中将参数return_inverse设置为True,那么它就是您要查找的输出:

a = np.array([1,2,1,1,1,2,3,1])
b = np.array([1,2,2,2,2,3,3,2])
_, inv = np.unique(np.stack([a,b]), axis=1, return_inverse=True)
print (inv)

array([0, 2, 1, 1, 1, 3, 4, 1], dtype=int64)

你可以把np.stack中的[a,b]替换为所有向量的列表。

编辑:一个更快的解决方案是在sum的数组上使用np.unique乘以max加上groupcols中所有先前数组的1的累积乘积(np.cumprod)。例如:

def group_np_sum(groupcols):
    groupcols_max = np.cumprod([ar.max()+1 for ar in groupcols[:-1]])
    return np.unique( sum([groupcols[0]] +
                          [ ar*m for ar, m in zip(groupcols[1:],groupcols_max)]), 
                      return_inverse=True)[1]

检查:

a = np.array([1,2,1,1,1,2,3,1])
b = np.array([1,2,2,2,2,3,3,2])
print (group_np_sum([a,b]))
array([0, 2, 1, 1, 1, 3, 4, 1], dtype=int64)

注意:与每个组相关联的数字可能不相同(这里我将a的第一个元素更改为3)

a = np.array([3,2,1,1,1,2,3,1])
b = np.array([1,2,2,2,2,3,3,2])
print(group_np2([a,b]))
print (group_np_sum([a,b]))
array([3, 1, 0, 0, 0, 2, 4, 0], dtype=int64)
array([0, 2, 1, 1, 1, 3, 4, 1], dtype=int64)

但群体本身是一样的。
现在检查一下时间:

a = np.random.randint(1, 100, 30000)
b = np.random.randint(1, 100, 30000)
c = np.random.randint(1, 100, 30000)
groupcols = [a,b,c]

%timeit group_pd(groupcols)
#13.7 ms ± 1.22 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit group_np2(groupcols)
#34.2 ms ± 6.88 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit group_np_sum(groupcols)
#3.63 ms ± 562 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
w6mmgewl

w6mmgewl2#

numpy_indexed包(dsiclaimer:我是它的作者)涵盖了这些类型的用例:

import numpy_indexed as npi
npi.group_by((a, b))

像这样传递一个索引数组的元组可以避免创建一个副本;但如果你不介意复制,你也可以使用堆叠:

npi.group_by(np.stack(a, b))
wh6knrhe

wh6knrhe3#

我写了一个group_by函数(here)来解决另一个问题。该功能非常灵活,可以解决您要求的问题:

from itertools import count

# First test case:
a = np.array([1, 1, 1, 2, 2, 3])
b = np.array([1, 2, 2, 2, 3, 3])
data = np.stack([a, b], axis=-1)
ids = count()
print(*group_by(data, lambda _: next(ids), transform=True)) # [0 1 1 2 3 4]

# Second test case:
a = np.array([1, 2, 1, 1, 1, 2, 3, 1])
b = np.array([1, 2, 2, 2, 2, 3, 3, 2])
data = np.stack([a, b], axis=-1)
ids = count()
second, = group_by(data, lambda _: next(ids), transform=True)
print(second, second[[2, 3, 4, 7]]) # [0 2 1 1 1 3 4 1] [1 1 1 1]

我想指出的是,公认的答案并没有回答你关于列举这些群体的问题。它在计算逆指数。但它确实突出了np.unique的参数axis,这是最关键的一点。

相关问题