将numpy数组项放入“bin”

时间:2017-11-15 14:45:57

标签: python arrays numpy

我有一个带有一些整数的numpy数组,例如,

a = numpy.array([1, 6, 6, 4, 1, 1, 4])

我现在想将所有项目放入具有相等值的“bins”中,以使标签为1的bin包含a的所有索引,其值为1。对于上面的例子:

bins = {
    1: [0, 4, 5],
    6: [1, 2],
    4: [3, 6],
    }

uniquewhere的组合可以解决问题,

uniques = numpy.unique(a)
bins = {u: numpy.where(a == u)[0] for u in uniques}

但这似乎并不理想,因为唯一条目的数量可能很大。

3 个答案:

答案 0 :(得分:2)

带附加的Defaultdict可以解决问题:

from collections import defaultdict

d = defaultdict(list)

for ix, val in enumerate(a):
  d[val].append(ix)

答案 1 :(得分:1)

这是一种方法 -

def groupby_uniqueness_dict(a):
    sidx = a.argsort()
    b = a[sidx]
    cut_idx = np.flatnonzero(b[1:] != b[:-1])+1
    parts = np.split(sidx, cut_idx)
    out = dict(zip(b[np.r_[0,cut_idx]], parts))
    return out

通过避免使用np.split -

来提高效率
def groupby_uniqueness_dict_v2(a):
    sidx = a.argsort()  # use .tolist() for output dict values as lists
    b = a[sidx]
    cut_idx = np.flatnonzero(b[1:] != b[:-1])+1
    idxs = np.r_[0,cut_idx, len(b)+1]
    out = {b[i]:sidx[i:j] for i,j in zip(idxs[:-1], idxs[1:])}
    return out

示例运行 -

In [161]: a
Out[161]: array([1, 6, 6, 4, 1, 1, 4])

In [162]: groupby_uniqueness_dict(a)
Out[162]: {1: array([0, 4, 5]), 4: array([3, 6]), 6: array([1, 2])}

运行时测试

其他方法 -

from collections import defaultdict

def defaultdict_app(a): # @Grisha's soln
    d = defaultdict(list)
    for ix, val in enumerate(a):
        d[val].append(ix)
    return d

计时 -

案例#1:Dict值为数组

In [226]: a = np.random.randint(0,1000, 10000)

In [227]: %timeit defaultdict_app(a)
     ...: %timeit groupby_uniqueness_dict(a)
     ...: %timeit groupby_uniqueness_dict_v2(a)
100 loops, best of 3: 4.06 ms per loop
100 loops, best of 3: 3.06 ms per loop
100 loops, best of 3: 2.02 ms per loop

In [228]: a = np.random.randint(0,10000, 100000)

In [229]: %timeit defaultdict_app(a)
     ...: %timeit groupby_uniqueness_dict(a)
     ...: %timeit groupby_uniqueness_dict_v2(a)
10 loops, best of 3: 43.5 ms per loop
10 loops, best of 3: 29.1 ms per loop
100 loops, best of 3: 19.9 ms per loop

案例#2:Dict值为列表

In [238]: a = np.random.randint(0,1000, 10000)

In [239]: %timeit defaultdict_app(a)
     ...: %timeit groupby_uniqueness_dict(a)
     ...: %timeit groupby_uniqueness_dict_v2(a)
100 loops, best of 3: 4.15 ms per loop
100 loops, best of 3: 4.5 ms per loop
100 loops, best of 3: 2.44 ms per loop

In [240]: a = np.random.randint(0,10000, 100000)

In [241]: %timeit defaultdict_app(a)
     ...: %timeit groupby_uniqueness_dict(a)
     ...: %timeit groupby_uniqueness_dict_v2(a)
10 loops, best of 3: 57.5 ms per loop
10 loops, best of 3: 54.6 ms per loop
10 loops, best of 3: 34 ms per loop

答案 2 :(得分:1)

以下是使用广播np.where()np.split()的一种方式:

In [66]: unique = np.unique(a)

In [67]: rows, cols = np.where(unique[:, None] == a)

In [68]: indices = np.split(cols, np.where(np.diff(rows) != 0)[0] + 1)

In [69]: dict(zip(unique, indices))
Out[69]: {1: array([0, 4, 5]), 4: array([3, 6]), 6: array([1, 2])}