3

I have a numpy array with some integers, e.g.,

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

I would now like to put all items into "bins" of equal values such that the bin with label 1 contains all indices of a that have the value 1. For the above example:

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

A combination of unique and wheres does the trick,

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

but this doesn't seem ideal since the number of unique entries may be large.

BallpointBen
  • 9,406
  • 1
  • 32
  • 62
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249

3 Answers3

2

Defaultdict with append would do the trick:

from collections import defaultdict

d = defaultdict(list)

for ix, val in enumerate(a):
  d[val].append(ix)
Grisha
  • 423
  • 4
  • 8
1

Here's one approach -

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

More efficient one by avoiding the use of 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

Sample run -

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])}

Runtime test

Other approach(es) -

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

Timings -

Case #1 : Dict values as arrays

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

Case #2 : Dict values as lists

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
Divakar
  • 218,885
  • 19
  • 262
  • 358
1

Here is one way by utilizing the broadcasting, np.where(), and 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])}
Mazdak
  • 105,000
  • 18
  • 159
  • 188