python - 检查两个 3D numpy 数组是否包含重叠的 2D 数组

标签 python arrays numpy multidimensional-array numpy-ndarray

我有两个非常大的 numpy 数组,它们都是 3D 的。我需要找到一种有效的方法来检查它们是否重叠,因为首先将它们都变成集合需要太长时间。我尝试使用我在此处找到的另一种解决方案来解决同样的问题,但适用于 2D 阵列,但我无法使其适用于 3D。 这是 2D 的解决方案:

nrows, ncols = A.shape
dtype={'names':['f{}'.format(i) for i in range(ndep)],
       'formats':ndep * [A.dtype]}
C = np.intersect1d(A.view(dtype).view(dtype), B.view(dtype).view(dtype))
# This last bit is optional if you're okay with "C" being a structured array...
C = C.view(A.dtype).reshape(-1, ndep)

(其中 A 和 B 是二维数组) 我需要找到重叠的 numpy 数组的数量,而不是特定的数组。

最佳答案

我们可以使用我在几个问答中使用过的辅助函数来利用 View 。要获得子数组的存在,我们可以在 View 上使用 np.isin 或使用更费力的 np.searchsorted

方法 #1: 使用 np.isin -

# https://stackoverflow.com/a/45313353/ @Divakar
def view1D(a, b): # a, b are arrays
    a = np.ascontiguousarray(a)
    b = np.ascontiguousarray(b)
    void_dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
    return a.view(void_dt).ravel(),  b.view(void_dt).ravel()

def isin_nd(a,b):
    # a,b are the 3D input arrays to give us "isin-like" functionality across them
    A,B = view1D(a.reshape(a.shape[0],-1),b.reshape(b.shape[0],-1))
    return np.isin(A,B)

方法 #2: 我们还可以在 views 上利用 np.searchsorted -

def isin_nd_searchsorted(a,b):
    # a,b are the 3D input arrays
    A,B = view1D(a.reshape(a.shape[0],-1),b.reshape(b.shape[0],-1))
    sidx = A.argsort()
    sorted_index = np.searchsorted(A,B,sorter=sidx)
    sorted_index[sorted_index==len(A)] = len(A)-1
    idx = sidx[sorted_index]
    return A[idx] == B

因此,这两个解决方案为我们提供了 a 中每个子数组在 b 中存在的掩码。因此,要获得我们想要的计数,它将是 - isin_nd(a,b).sum()isin_nd_searchsorted(a,b).sum()

sample 运行-

In [71]: # Setup with 3 common "subarrays"
    ...: np.random.seed(0)
    ...: a = np.random.randint(0,9,(10,4,5))
    ...: b = np.random.randint(0,9,(7,4,5))
    ...: 
    ...: b[1] = a[4]
    ...: b[3] = a[2]
    ...: b[6] = a[0]

In [72]: isin_nd(a,b).sum()
Out[72]: 3

In [73]: isin_nd_searchsorted(a,b).sum()
Out[73]: 3

大型阵列上的计时 -

In [74]: # Setup
    ...: np.random.seed(0)
    ...: a = np.random.randint(0,9,(100,100,100))
    ...: b = np.random.randint(0,9,(100,100,100))
    ...: idxa = np.random.choice(range(len(a)), len(a)//2, replace=False)
    ...: idxb = np.random.choice(range(len(b)), len(b)//2, replace=False)
    ...: a[idxa] = b[idxb]

# Verify output
In [82]: np.allclose(isin_nd(a,b),isin_nd_searchsorted(a,b))
Out[82]: True

In [75]: %timeit isin_nd(a,b).sum()
10 loops, best of 3: 31.2 ms per loop

In [76]: %timeit isin_nd_searchsorted(a,b).sum()
100 loops, best of 3: 1.98 ms per loop

关于python - 检查两个 3D numpy 数组是否包含重叠的 2D 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54791950/

相关文章:

python - 将图边列表转换为 JSON 树

python - Django 表单的赞成票和反对票按钮

javascript - 从数组 Angular js中删除未经检查的值

python - 如何确定 PyObject 是否为 numpy PyArrayObject

python - 将字典中的值从 str 转换为 float

java - 计算多篇文章中的关键字出现次数

javascript - 如何在 Javascript 中为复杂的对象数组链接映射和过滤方法?

python - numpy - 返回数组中元素的第一个索引

python - 如何检查 numpy 数组的所有元素是否都具有整数值?

python - 错误 'module' 对象没有属性 'freetype'