簡體   English   中英

如果數組出現在純numpy的另一個數組中,則有效地刪除數組的每一行

[英]Efficiently delete each row of an array if it occurs in another array in pure numpy

我有一個numpy數組,其中索引以(n, 2)的形式存儲。 例如:

[[0, 1],
 [2, 3], 
 [1, 2], 
 [4, 2]]

然后我做一些處理並創建一個形狀為(m, 2)的數組,其中n > m 例如:

[[2, 3]
 [4, 2]]

現在我想刪除第一個數組中可以在第二個數組中找到的每一行。 所以我想要的結果是:

[[0, 1], 
 [1, 2]]

我目前的解決方案如下:

for row in second_array:
        result = np.delete(first_array, np.where(np.all(first_array == second_array, axis=1)), axis=0)

然而,如果第二個很大,這是安靜的時間。 有人知道一個只有numpy的解決方案,它不需要循環嗎?

這里有一個利用matrix-multiplication減少維數的正數的事實 -

def setdiff_nd_positivenums(a,b):
    s = np.maximum(a.max(0)+1,b.max(0)+1)
    return a[~np.isin(a.dot(s),b.dot(s))]

樣品運行 -

In [82]: a
Out[82]: 
array([[0, 1],
       [2, 3],
       [1, 2],
       [4, 2]])

In [83]: b
Out[83]: 
array([[2, 3],
       [4, 2]])

In [85]: setdiff_nd_positivenums(a,b)
Out[85]: 
array([[0, 1],
       [1, 2]])

另外,似乎在第二陣列b的一個子集a 因此,我們可以利用np.searchsorted進一步提升性能,如此 -

def setdiff_nd_positivenums_searchsorted(a,b):
    s = np.maximum(a.max(0)+1,b.max(0)+1)
    a1D,b1D = a.dot(s),b.dot(s)
    b1Ds = np.sort(b1D)
    return a[b1Ds[np.searchsorted(b1Ds,a1D)] != a1D]

計時 -

In [146]: np.random.seed(0)
     ...: a = np.random.randint(0,9,(1000000,2))
     ...: b = a[np.random.choice(len(a), 10000, replace=0)]

In [147]: %timeit setdiff_nd_positivenums(a,b)
     ...: %timeit setdiff_nd_positivenums_searchsorted(a,b)
10 loops, best of 3: 101 ms per loop
10 loops, best of 3: 70.9 ms per loop

對於通用數字,這是另一個使用views -

# 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 setdiff_nd(a,b):
    # a,b are the nD input arrays
    A,B = view1D(a,b)    
    return a[~np.isin(A,B)]

樣品運行 -

In [94]: a
Out[94]: 
array([[ 0,  1],
       [-2, -3],
       [ 1,  2],
       [-4, -2]])

In [95]: b
Out[95]: 
array([[-2, -3],
       [ 4,  2]])

In [96]: setdiff_nd(a,b)
Out[96]: 
array([[ 0,  1],
       [ 1,  2],
       [-4, -2]])

計時 -

In [158]: np.random.seed(0)
     ...: a = np.random.randint(0,9,(1000000,2))
     ...: b = a[np.random.choice(len(a), 10000, replace=0)]

In [159]: %timeit setdiff_nd(a,b)
1 loop, best of 3: 352 ms per loop

這是一個函數,它可以處理任何形狀的2D整數數組,並接受正數和負數:

import numpy as np

# Gets a boolean array of rows of a that are in b
def isin_rows(a, b):
    a = np.asarray(a)
    b = np.asarray(b)
    # Subtract minimum value per column
    min = np.minimum(a.min(0), b.min(0))
    a = a - min
    b = b - min
    # Get maximum value per column
    max = np.maximum(a.max(0), b.max(0))
    # Compute multiplicative base for each column
    base = np.roll(max, 1)
    base[0] = 1
    base = np.cumprod(max)
    # Make flattened version of arrays
    a_flat = (a * base).sum(1)
    b_flat = (b * base).sum(1)
    # Check elements of a in b
    return np.isin(a_flat, b_flat)

# Test
a = np.array([[0, 1],
              [2, 3],
              [1, 2],
              [4, 2]])
b = np.array([[2, 3],
              [4, 2]])
a_in_b_mask = isin_rows(a, b)
a_not_in_b = a[~a_in_b_mask]
print(a_not_in_b)
# [[0 1]
#  [1 2]]

編輯:一個可能的優化從考慮b中可能的行數增加。 如果b行數多於可能的組合數,那么您可能首先找到其唯一元素,因此np.isin更快:

import numpy as np

def isin_rows_opt(a, b):
    a = np.asarray(a)
    b = np.asarray(b)
    min = np.minimum(a.min(0), b.min(0))
    a = a - min
    b = b - min
    max = np.maximum(a.max(0), b.max(0))
    base = np.roll(max, 1)
    base[0] = 1
    base = np.cumprod(max)
    a_flat = (a * base).sum(1)
    b_flat = (b * base).sum(1)
    # Count number of possible different rows for b
    num_possible_b = np.prod(b.max(0) - b.min(0) + 1)
    if len(b_flat) > num_possible_b:  # May tune this condition
        b_flat = np.unique(b_flat)
    return np.isin(a_flat, b_flat)

條件len(b_flat) > num_possible_b應該可以更好地調整,因此如果它真的值得(例如len(b_flat) > 2 * num_possible_blen(b_flat) > num_possible_b + CONSTANT ),你只能找到唯一元素。 它似乎為具有較少值的大數組提供了一些改進:

import numpy as np

# Test setup from @Divakar
np.random.seed(0)
a = np.random.randint(0, 9, (1000000, 2))
b = a[np.random.choice(len(a), 10000, replace=0)]
print(np.all(isin_rows(a, b) == isin_rows_opt(a, b)))
# True
%timeit isin_rows(a, b)
# 100 ms ± 425 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit isin_rows_opt(a, b)
# 81.2 ms ± 324 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

numpy-indexed包(免責聲明:我是它的作者)被設計為在nd-arrays上有效地執行這種類型的操作。

import numpy_indexed as npi
# if the output should consist of unique values and there is no need to preserve ordering
result = npi.difference(first_array, second_array)
# otherwise:
result = first_array[~npi.in_(first_array, second_array)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM