当前位置: 首页 > 面试题库 >

测试2d numpy数组中的成员资格

越嘉茂
2023-03-14
问题内容

我有两个相同大小的2D阵列

a = array([[1,2],[3,4],[5,6]])
b = array([[1,2],[3,4],[7,8]])

我想知道a中b的行。

因此输出应为:

array([ True,  True, False], dtype=bool)

没有做:

array([any(i == a) for i in b])

因为a和b很大。

有一个函数可以执行此操作,但仅适用于一维数组:in1d


问题答案:

我们真正想做的是使用np.in1d…,除了np.in1d仅适用于一维数组。我们的数组是多维的。然而,我们可以 看到 阵列为的1维阵列
的字符串

arr.view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[-1])))

例如,

In [15]: arr = np.array([[1, 2], [2, 3], [1, 3]])

In [16]: arr = arr.view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[-1])))

In [30]: arr.dtype
Out[30]: dtype('V16')

In [31]: arr.shape
Out[31]: (3, 1)

In [37]: arr
Out[37]: 
array([[b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00'],
       [b'\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'],
       [b'\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00']],
      dtype='|V16')

这使得arr字符串的每一行。现在,只需要将其连接到np.in1d

import numpy as np

def asvoid(arr):
    """
    Based on http://stackoverflow.com/a/16973510/190597 (Jaime, 2013-06)
    View the array as dtype np.void (bytes). The items along the last axis are
    viewed as one value. This allows comparisons to be performed on the entire row.
    """
    arr = np.ascontiguousarray(arr)
    if np.issubdtype(arr.dtype, np.floating):
        """ Care needs to be taken here since
        np.array([-0.]).view(np.void) != np.array([0.]).view(np.void)
        Adding 0. converts -0. to 0.
        """
        arr += 0.
    return arr.view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[-1])))


def inNd(a, b, assume_unique=False):
    a = asvoid(a)
    b = asvoid(b)
    return np.in1d(a, b, assume_unique)


tests = [
    (np.array([[1, 2], [2, 3], [1, 3]]),
     np.array([[2, 2], [3, 3], [4, 4]]),
     np.array([False, False, False])),
    (np.array([[1, 2], [2, 2], [1, 3]]),
     np.array([[2, 2], [3, 3], [4, 4]]),
     np.array([True, False, False])),
    (np.array([[1, 2], [3, 4], [5, 6]]),
     np.array([[1, 2], [3, 4], [7, 8]]),
     np.array([True, True, False])),
    (np.array([[1, 2], [5, 6], [3, 4]]),
     np.array([[1, 2], [5, 6], [7, 8]]),
     np.array([True, True, False])),
    (np.array([[-0.5, 2.5, -2, 100, 2], [5, 6, 7, 8, 9], [3, 4, 5, 6, 7]]),
     np.array([[1.0, 2, 3, 4, 5], [5, 6, 7, 8, 9], [-0.5, 2.5, -2, 100, 2]]),
     np.array([False, True, True]))
]

for a, b, answer in tests:
    result = inNd(b, a)
    try:
        assert np.all(answer == result)
    except AssertionError:
        print('''\
a:
{a}
b:
{b}

answer: {answer}
result: {result}'''.format(**locals()))
        raise
else:
    print('Success!')

产量

Success!


 类似资料:
  • 问题内容: 5 in [1, 2, 3, 4] == False False 我知道这是测试会员资格的一种奇怪方法,并且 是“正确”的方式。令我感到困惑的是,它的行为不同于两者 和 我错过了明显的事情吗?(在Python 2.7和Python 3.4中测试)。 为了澄清,我理解了最后三个片段。我在问 第一个 代码片段的行为,以及为什么与众不同。 问题答案: 这是一个链式比较。您可能知道您可以做 在

  • 我目前正在尝试遍历二叉树以检查某些值是否在其中。for 循环正在测试 1 到 50 之间的所有值,并将为每个匹配的值返回 true。 这是当前的树: 现在我必须实现成员函数,我有正确的想法下来,但它在检查根后停止,根 - 前面所述的for循环打印输出 但仅此而已。 有人会通过伪代码或脚本提供一些方向吗?你不必给我代码,因为我想自己弄清楚。谢谢。

  • 有没有办法在开发者控制台中从我的Google Play beta测试组中删除成员?我已经把他们从我的谷歌社区中删除了,当用户检查他们的G社区时,我的没有被列出。然而,当他们去商店搜索我的应用程序时,他们仍然会看到并下载测试版。

  • 我正在测试我的状态。我读到: 但是我得到一个异常 据我所知,生产线小车是: $form=$this- 但我该如何解决? 我宣布: PHPUnit测试/Unit/Form/MediaTypeTest.php 或通过代码欺骗: php供应商/bin/codecept运行单元表单/MediaTypeTest.php 有什么想法吗?

  • 背景:我们使用书面记录来记录我们不断变化的模型的历史。现在我想查询一个属于某个客户的项目。PaperTrail可选择存储对象更改,我需要查询此字段以了解何时使用此ID创建了某些内容或更改为此ID。 我的表看起来简化如下: 如何查询从或更改为ID 5的元素(因此上面的所有行)?我试过: 这让我:

  • 我的应用程序还没有发布。通过Google Play测试应用程序的Alpha/Beta测试者的最大数量是多少?对于iOS来说,每个构建需要1000个外部测试人员。Google Play的限制是多少?