在2D和1D数组之间逐元素使用numpy isin [英] Using numpy isin element-wise between 2D and 1D arrays
问题描述
我有一个非常简单的场景,我想测试二维数组的两个元素是否(分别)是较大数组的成员-例如:
I have quite a simple scenario where I'd like to test whether both elements of a two-dimensional array are (separately) members of a larger array - for example:
full_array = np.array(['A','B','C','D','E','F'])
sub_arrays = np.array([['A','C','F'],
['B','C','E']])
np.isin(full_array, sub_arrays)
这给了我一个一维的输出:
This gives me a single dimension output:
array([ True, True, True, False, True, True])
显示full_array的元素是否存在于两个子数组中的任何一个中.相反,我想要一个二维数组,它为sub_arrays中的两个元素中的每个元素显示相同的内容-所以:
showing whether elements of full_array are present in either of the two sub-arrays. I'd like instead a two-dimensional array showing the same thing for each of the two elements in sub_arrays - so:
array([[ True, False, True, False, False, True],
[ False, True, True, False, True, False]])
希望如此,希望得到的任何帮助.
Hope that makes sense and any help gratefully received.
推荐答案
基于广播的广播
一个简单的方法就是 broadcasting
扩展数组之一,然后沿各自的轴任意缩小之后-
Broadcasting based one
A simple one would be with broadcasting
after extending one of the arrays and then any-reduction along the respective axis -
In [140]: (full_array==sub_arrays[...,None]).any(axis=1)
Out[140]:
array([[ True, False, True, False, False, True],
[False, True, True, False, True, False]])
使用searchsorted
特定案例#1
With searchsorted
Specific case #1
对full_array
进行排序,并且sub_arrays
中的所有元素至少出现在full_array
中的某个位置,我们也可以使用np.searchsorted
-
With full_array
being sorted and all elements from sub_arrays
present at least somewhere in full_array
, we can also use np.searchsorted
-
idx = np.searchsorted(full_array, sub_arrays)
out = np.zeros((sub_arrays.shape[0],len(full_array)),dtype=bool)
np.put_along_axis(out, idx, 1, axis=1)
特定案例#2
对full_array
进行排序,并且如果不是所有sub_arrays
中的元素都保证至少存在于full_array
中的某个地方,我们需要执行一个额外的步骤-
With full_array
being sorted and if not all elements from sub_arrays
are guaranteed to be present at least somewhere in full_array
, we need one extra step -
idx = np.searchsorted(full_array, sub_arrays)
idx[idx==len(full_array)] = 0
out = np.zeros((sub_arrays.shape[0],len(full_array)),dtype=bool)
np.put_along_axis(out, idx, full_array[idx] == sub_arrays, axis=1)
一般情况
对于full_array
的真正通用情况(不一定要排序),我们需要将sorter
arg与searchsorted
-
For the truly generic case of full_array
not necessarily being sorted, we need to use sorter
arg with searchsorted
-
def isin2D(full_array, sub_arrays):
out = np.zeros((sub_arrays.shape[0],len(full_array)),dtype=bool)
sidx = full_array.argsort()
idx = np.searchsorted(full_array, sub_arrays, sorter=sidx)
idx[idx==len(full_array)] = 0
idx0 = sidx[idx]
np.put_along_axis(out, idx0, full_array[idx0] == sub_arrays, axis=1)
return out
样品运行-
In [214]: full_array
Out[214]: array(['E', 'F', 'A', 'B', 'D', 'C'], dtype='|S1')
In [215]: sub_arrays
Out[215]:
array([['Z', 'C', 'F'],
['B', 'C', 'E']], dtype='|S1')
In [216]: isin2D(full_array, sub_arrays)
Out[216]:
array([[False, True, False, False, False, True],
[ True, False, False, True, False, True]])
这篇关于在2D和1D数组之间逐元素使用numpy isin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!