繁体   English   中英

Map 多维数组中的一个元素到它的索引

[英]Map an element in a multi-dimension array to its index

我正在使用 function get_tuples(length, total)这里生成给定长度和总和的所有元组的数组,一个示例和 function 如下所示。 创建数组后,我需要找到一种方法来返回数组中给定数量元素的索引。 通过将数组更改为列表,我可以使用.index()来做到这一点,如下所示。 但是,此解决方案或其他基于搜索的解决方案(例如使用np.where )需要大量时间来查找索引。 由于数组中的所有元素(示例中的数组s )都是不同的,我想知道我们是否可以构造一个一对一的映射,即 function 使得给定数组中的元素它返回元素的索引通过对该元素的值进行一些加法和乘法。 如果可能的话,有什么想法吗? 谢谢!

import numpy as np

def get_tuples(length, total):
    if length == 1:
        yield (total,)
        return

    for i in range(total + 1):
        for t in get_tuples(length - 1, total - i):
            yield (i,) + t
#example
s = np.array(list(get_tuples(4, 20)))

# array s
In [1]: s
Out[1]: 
array([[ 0,  0,  0, 20],
       [ 0,  0,  1, 19],
       [ 0,  0,  2, 18],
       ...,
       [19,  0,  1,  0],
       [19,  1,  0,  0],
       [20,  0,  0,  0]])

#example of element to find the index for. (Note in reality this is 1000+ elements)
elements_to_find =np.array([[ 0,  0,  0, 20],
                            [ 0,  0,  7, 13],
                            [ 0,  5,  5, 10],
                            [ 0,  0,  5, 15],
                            [ 0,  2,  4, 14]])
#change array to list
s_list = s.tolist()

#find the indices
indx=[s_list.index(i) for i in elements_to_find.tolist()]

#output
In [2]: indx
Out[2]: [0, 7, 100, 5, 45]

这是一个仅基于元组计算索引的公式,即它不需要看到完整的数组。 要计算 N 元组的索引,它需要评估 N-1 个二项式系数。 以下实现是(部分)矢量化的,它接受 ND 数组,但元组必须在最后一维中。

import numpy as np
from scipy.special import comb

# unfortunately, comb with option exact=True is not vectorized
def bc(N,k):
    return np.round(comb(N,k)).astype(int)

def get_idx(s):
    N = s.shape[-1] - 1
    R = np.arange(1,N)
    ps = s[...,::-1].cumsum(-1)
    B = bc(ps[...,1:-1]+R,1+R)
    return bc(ps[...,-1]+N,N) - ps[...,0] - 1 - B.sum(-1)

# OP's generator
def get_tuples(length, total):
    if length == 1:
        yield (total,)
        return

    for i in range(total + 1):
        for t in get_tuples(length - 1, total - i):
            yield (i,) + t
#example
s = np.array(list(get_tuples(4, 20)))

# compute each index
r = get_idx(s)

# expected: 0,1,2,3,...
assert (r == np.arange(len(r))).all()
print("all ok")

#example of element to find the index for. (Note in reality this is 1000+ elements)
elements_to_find =np.array([[ 0,  0,  0, 20],
                            [ 0,  0,  7, 13],
                            [ 0,  5,  5, 10],
                            [ 0,  0,  5, 15],
                            [ 0,  2,  4, 14]])

print(get_idx(elements_to_find))

样品运行:

all ok
[  0   7 100   5  45]

如何推导出公式:

  1. 使用星形和条形表示完整的分区计数#part(N,k) (N 是总数,k 是长度)作为单个二项式系数(N + k - 1) choose (k - 1)

  2. 从后到前计数:不难验证在 OP 生成器的外循环的第 i 次完整迭代之后, #part(Ni,k)还没有被枚举。 实际上,剩下的就是所有分区 p1+p2+... = N with p1>=i; 我们可以写 p1=q1+i 使得 q1+p2+... = Ni 并且后一个分区是无约束的,所以我们可以使用 1. 来计数。

您可以使用二分搜索来加快搜索速度。

二分搜索使搜索 O(log(n)) 而不是 O(n)(使用索引)

我们不需要对元组进行排序,因为它们已经由生成器排序

import bisect

def get_tuples(length, total):
  " Generates tuples "
  if length == 1:
    yield (total,)
    return

  yield from ((i,) + t for i in range(total + 1) for t in get_tuples(length - 1, total - i))

def find_indexes(x, indexes):
   if len(indexes) > 100:
        # Faster to generate all indexes when we have a large
        # number to check
        d = dict(zip(x, range(len(x))))
        return [d[tuple(i)] for i in indexes]
    else:
        return [bisect.bisect_left(x, tuple(i)) for i in indexes]

# Generate tuples (in this case 4, 20)
x = list(get_tuples(4, 20))

# Tuples are generated in sorted order [(0,0,0,20), ...(20,0,0,0)]
# which allows binary search to be used
indexes = [[ 0,  0,  0, 20],
           [ 0,  0,  7, 13],
           [ 0,  5,  5, 10],
           [ 0,  0,  5, 15],
           [ 0,  2,  4, 14]]

y = find_indexes(x, indexes)
print('Found indexes:', *y)
print('Indexes & Tuples:')
for i in y:
  print(i, x[i])

Output

Found indexes: 0 7 100 5 45
Indexes & Tuples:
0 (0, 0, 0, 20)
7 (0, 0, 7, 13)
100 (0, 5, 5, 10)
5 (0, 0, 5, 15)
45 (0, 2, 4, 14)

表现

场景1——元组已经计算,我们只想找到某些元组的索引

例如 x = list(get_tuples(4, 20)) 已经被执行。

搜索

indexes = [[ 0,  0,  0, 20],
           [ 0,  0,  7, 13],
           [ 0,  5,  5, 10],
           [ 0,  0,  5, 15],
           [ 0,  2,  4, 14]]

二进制搜索

%timeit find_indexes(x, indexes)
100000 loops, best of 3: 11.2 µs per loop

仅基于元组计算索引(礼貌@PaulPanzer 方法)

%timeit get_idx(indexes)
10000 loops, best of 3: 92.7 µs per loop

在这种情况下,当元组已经被预先计算时,二分查找的速度要快 8 倍。

场景 2——元组没有被预先计算。

%%timeit
import bisect

def find_indexes(x, t):
    " finds the index of each tuple in list t (assumes x is sorted) "
    return [bisect.bisect_left(x, tuple(i)) for i in t]

# Generate tuples (in this case 4, 20)
x = list(get_tuples(4, 20))

indexes = [[ 0,  0,  0, 20],
           [ 0,  0,  7, 13],
           [ 0,  5,  5, 10],
           [ 0,  0,  5, 15],
           [ 0,  2,  4, 14]]

y = find_indexes(x, indexes)

100 loops, best of 3: 2.69 ms per loop

@PaulPanzer 方法在这种情况下是相同的时间(92.97 us)

=> @PaulPanzer 在不必计算元组时方法快约 29 倍

场景3——大量索引(@PJORR)大量随机索引产生

x = list(get_tuples(4, 20))
xnp = np.array(x)
indices = xnp[np.random.randint(0,len(xnp), 2000)]
indexes = indices.tolist()
%timeit find_indexes(x, indexes)
#Result: 1000 loops, best of 3: 1.1 ms per loop
%timeit get_idx(indices)
#Result: 1000 loops, best of 3: 716 µs per loop

在这种情况下,我们 @PaulPanzer 快了 53%

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM