2017-04-23 5 views
6

問題は非常に単純です:2つの2d np.arrayがあり、後者の2つの行に共通しない行しか含まない3番目の配列を取得したいとします。Numpy:2行に共通の行を削除する方法

例えば

:私はnp.delete(X,Y,axis=0)を試みたが、それは動作しません

X = np.array([[0,1],[1,2],[4,5],[5,6],[8,9],[9,10]]) 
Y = np.array([[5,6],[9,10]]) 

Z = function(X,Y) 
Z = array([[0, 1], 
      [1, 2], 
      [4, 5], 
      [8, 9]]) 

...

答えて

2
Z = np.vstack(row for row in X if row not in Y) 
+0

シンプルでエレガント!ありがとう – rugrag

+0

このソリューションは、両方のセットのサイズの積に等しい操作の数を必要とすることに注意してください。これは理想とはかけ離れています。 –

+0

@EelcoHoogendoornでも、 '' Z = npi.difference(X、Y) ''よりも約10倍高速です。自分で確認できます:) – Luchko

-1
Z = np.unique([tuple(row) for row in X + Y]) 
0

ここviewsベースのアプローチだ -

# Based on http://stackoverflow.com/a/41417343/3293881 by @Eric 
def setdiff2d(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) 

    # Get indices in a that are also in b 
    return np.setdiff1d(a_void, b_void).view(orig_dt) 

サンプル実行 -

In [81]: X 
Out[81]: 
array([[ 0, 1], 
     [ 1, 2], 
     [ 4, 5], 
     [ 5, 6], 
     [ 8, 9], 
     [ 9, 10]]) 

In [82]: Y 
Out[82]: 
array([[ 5, 6], 
     [ 9, 10]]) 

In [83]: setdiff2d(X,Y) 
Out[83]: 
array([[0, 1], 
     [1, 2], 
     [4, 5], 
     [8, 9]]) 
1

numpy_indexedパッケージ(免責事項:私はその作者午前)を効率よく、このようなユースケース次元のマルチために、標準のnumpyの配列のセット操作を拡張:

import numpy_indexed as npi 
Z = npi.difference(X, Y) 
関連する問題