13

同じサイズの 2 つの 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 が大きいからです。

これを行う関数がありますが、1D 配列に対してのみです: in1d

4

4 に答える 4

13

私たちが本当にやりたいのはnp.in1d... を使用することですが、それはnp.in1d1 次元配列でのみ機能します。私たちの配列は多次元です。ただし、配列を文字列の 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!
于 2013-04-25T14:03:09.463 に答える
2

a=np.array([[1,2],[3,4],[5,6]])とのようなものがb=np.array([[5,6],[1,2],[7,6]])ある場合は、それらを複雑な 1 次元配列に変換できます。

c=a[:,0]+a[:,1]*1j
d=b[:,0]+b[:,1]*1j

私のインタープリターのこの全体は次のようになります。

>>> c=a[:,0]+a[:,1]*1j
>>> c
array([ 1.+2.j,  3.+4.j,  5.+6.j])
>>> d=b[:,0]+b[:,1]*1j
>>> d
array([ 5.+6.j,  1.+2.j,  7.+6.j])

これで 1D 配列だけになったので、簡単に を実行できますnp.in1d(c,d)。Python は次のように表示します。

>>> np.in1d(c,d)
array([ True, False,  True], dtype=bool)

これにより、少なくともこのデータ型では、ループは必要ありません

于 2016-07-20T11:01:37.820 に答える
0

numpy モジュールは実際に配列を介してブロードキャストし、どの部分が他の部分と同じであるかを伝え、そうでない場合は true を返し、そうでない場合は false を返すことができます。

import numpy as np
a = np.array(([1,2],[3,4],[5,6])) #converting to a numpy array
b = np.array(([1,2],[3,4],[7,8])) #converting to a numpy array
new_array = a == b #creating a new boolean array from comparing a and b

new_array は次のようになります。

[[ True  True]
 [ True  True]
 [False False]]

しかし、それはあなたが望むものではありません。したがって、配列を転置 (x と y を反転) してから、2 つの行を&ゲートと比較できます。これにより、行の両方の列が true の場合にのみ true を返す 1 次元配列が作成されます。

new_array = new_array.T #transposing
result = new_array[0] & new_array[1] #comparing rows

print result探しているものを手に入れたら:

[ True  True False]
于 2013-04-25T14:16:36.800 に答える