Numpy finding element index in another array
我有一个具有唯一正整数的数组/集合,即
1  | >>> unique = np.unique(np.random.choice(100, 4, replace=False))  | 
以及包含从上一个数组中采样的多个元素的数组,例如
1  | >>> A = np.random.choice(unique, 100)  | 
号
我想把数组
到目前为止,我找到的最佳解决方案是通过一个映射数组:
1 2  | >>> table = np.zeros(unique.max()+1, unique.dtype) >>> table[unique] = np.arange(unique.size)  | 
上面为每个元素分配了数组上的索引,因此可以稍后通过高级索引来映射
1 2 3 4 5 6  | >>> table[A] array([2, 2, 3, 3, 3, 3, 1, 1, 1, 0, 2, 0, 1, 0, 2, 1, 0, 0, 2, 3, 0, 0, 0, 0, 3, 3, 2, 1, 0, 0, 0, 2, 1, 0, 3, 0, 1, 3, 0, 1, 2, 3, 3, 3, 3, 1, 3, 0, 1, 2, 0, 0, 2, 3, 1, 0, 3, 2, 3, 3, 3, 1, 1, 2, 0, 0, 2, 0, 2, 3, 1, 1, 3, 3, 2, 1, 2, 0, 2, 1, 0, 1, 2, 0, 2, 0, 1, 3, 0, 2, 0, 1, 3, 2, 2, 1, 3, 0, 3, 3], dtype=int32)  | 
。
这已经给了我正确的解决方案。但是,如果
有更好的解决办法吗?
注:
1 2 3  | B = np.zeros_like(A) for i in range(A.size): B[i] = unique.index(A[i])  | 
(假设
当
numpy_索引包(免责声明:我是其作者)包含一个与list.index等价的矢量化包,它不需要与max元素成比例的内存,但只与输入本身成比例:
1 2  | import numpy_indexed as npi npi.indices(unique, A)  | 
号
请注意,它也适用于任意数据类型和维度。另外,所查询的数组不需要是唯一的;将返回遇到的第一个索引,与for list相同。
您可以将标准的python 
1 2  | inds = {e:i for i, e in enumerate(unique)} B = np.vectorize(inds.get)(A)  |