以矩阵为元素的数组的块状相交 [英] Numpy intersect1d with array with matrix as elements

查看:57
本文介绍了以矩阵为元素的数组的块状相交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组,一个数组的形状为(200000, 28, 28),另一个数组的形状为(10000, 28, 28),因此实际上是两个数组,其中矩阵为元素. 现在,我想计算并获得在两个数组中重叠的所有元素(格式为(N, 28, 28)).对于普通的for循环,这是一种减慢速度的方法,因此我尝试使用numpys intersect1d方法对其进行了尝试,但我不知道如何将其应用于此类数组.

I have two arrays, one of the shape (200000, 28, 28) and the other of the shape (10000, 28, 28), so practically two arrays with matrices as elements. Now I want to count and get all the elements (in the form (N, 28, 28)), that overlap in both arrays. With normal for loops it is way to slow, so I tryied it with numpys intersect1d method, but I dont know how to apply it on this types of arrays.

推荐答案

使用关于唯一行的问题

def intersect_along_first_axis(a, b):
    # check that casting to void will create equal size elements
    assert a.shape[1:] == b.shape[1:]
    assert a.dtype == b.dtype

    # compute dtypes
    void_dt = np.dtype((np.void, a.dtype.itemsize * np.prod(a.shape[1:])))
    orig_dt = np.dtype((a.dtype, a.shape[1:]))

    # convert to 1d void arrays
    a = np.ascontiguousarray(a)
    b = np.ascontiguousarray(b)
    a_void = a.reshape(a.shape[0], -1).view(void_dt)
    b_void = b.reshape(b.shape[0], -1).view(void_dt)

    # intersect, then convert back
    return np.intersect1d(b_void, a_void).view(orig_dt)

请注意,对浮点数使用void是不安全的,因为这会导致-00不相等

Note that using void is unsafe with floats, as it will cause -0 to be unequal to 0

这篇关于以矩阵为元素的数组的块状相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆