Using numpy isin element-wise between 2D and 1D arrays

 ̄綄美尐妖づ 提交于 2020-07-03 02:46:23

问题


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

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.


回答1:


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

With searchsorted

Specific case #1

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)

Specific case #2

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)

Generic case

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

Sample run -

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


来源:https://stackoverflow.com/questions/53631460/using-numpy-isin-element-wise-between-2d-and-1d-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!