删除numpy数组的重复行

hik*_*ker 32 python numpy

如何删除二维numpy数组的重复行?

data = np.array([[1,8,3,3,4],
                 [1,8,9,9,4],
                 [1,8,3,3,4]])
Run Code Online (Sandbox Code Playgroud)

答案应如下:

ans = array([[1,8,3,3,4],
             [1,8,9,9,4]])
Run Code Online (Sandbox Code Playgroud)

如果有两行相同,那么我想删除一个"重复"行.

The*_*tor 39

你可以用numpy unique.由于您需要唯一的行,我们需要将它们放入元组中:

import numpy as np

data = np.array([[1,8,3,3,4],
                 [1,8,9,9,4],
                 [1,8,3,3,4]])
Run Code Online (Sandbox Code Playgroud)

只是应用np.uniquedata阵列会导致这样的:

>>> uniques
array([1, 3, 4, 8, 9])
Run Code Online (Sandbox Code Playgroud)

打印出列表中的唯一元素.因此将它们放入元组会导致:

new_array = [tuple(row) for row in data]
uniques = np.unique(new_array)
Run Code Online (Sandbox Code Playgroud)

打印:

>>> uniques
array([[1, 8, 3, 3, 4],
       [1, 8, 9, 9, 4]])
Run Code Online (Sandbox Code Playgroud)

  • 我试过`new_array = [tuple(row)for data in data] uniques = np.unique(new_array)`但它仍然输出唯一的`array([1,3,4,8,9])`@ThePredator (17认同)
  • 在新版本中,您需要设置`np.unique(data,axis = 0)` (5认同)
  • 这是代码,我使用与你的show相同的代码:`import numpy as np data = np.array([[1,8,3,3,4],[1,8,9,9,4], [1,8,3,3,4]])new_array = [数据中行的元组(行)] uniques = np.unique(new_array)uniques Out [30]:array([1,3,4,8, 9])`那是关于numpy版本的吗?我的numpy版本是1.9.2 (3认同)
  • 我认为以下是正确的答案http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array (2认同)

Div*_*kar 21

一种方法lex-sorting-

# Perform lex sort and get sorted data
sorted_idx = np.lexsort(data.T)
sorted_data =  data[sorted_idx,:]

# Get unique row mask
row_mask = np.append([True],np.any(np.diff(sorted_data,axis=0),1))

# Get unique rows
out = sorted_data[row_mask]
Run Code Online (Sandbox Code Playgroud)

样品运行 -

In [199]: data
Out[199]: 
array([[1, 8, 3, 3, 4],
       [1, 8, 9, 9, 4],
       [1, 8, 3, 3, 4],
       [1, 8, 3, 3, 4],
       [1, 8, 0, 3, 4],
       [1, 8, 9, 9, 4]])

In [200]: sorted_idx = np.lexsort(data.T)
     ...: sorted_data =  data[sorted_idx,:]
     ...: row_mask = np.append([True],np.any(np.diff(sorted_data,axis=0),1))
     ...: out = sorted_data[row_mask]
     ...: 

In [201]: out
Out[201]: 
array([[1, 8, 0, 3, 4],
       [1, 8, 3, 3, 4],
       [1, 8, 9, 9, 4]])
Run Code Online (Sandbox Code Playgroud)

运行时测试 -

本节介绍了迄今为止所提出的解决方案中提出的所有方法.

In [34]: data = np.random.randint(0,10,(10000,10))

In [35]: def tuple_based(data):
    ...:     new_array = [tuple(row) for row in data]
    ...:     return np.unique(new_array)
    ...: 
    ...: def lexsort_based(data):                 
    ...:     sorted_data =  data[np.lexsort(data.T),:]
    ...:     row_mask = np.append([True],np.any(np.diff(sorted_data,axis=0),1))
    ...:     return sorted_data[row_mask]
    ...: 
    ...: def unique_based(a):
    ...:     a = np.ascontiguousarray(a)
    ...:     unique_a = np.unique(a.view([('', a.dtype)]*a.shape[1]))
    ...:     return unique_a.view(a.dtype).reshape((unique_a.shape[0], a.shape[1]))
    ...: 

In [36]: %timeit tuple_based(data)
10 loops, best of 3: 63.1 ms per loop

In [37]: %timeit lexsort_based(data)
100 loops, best of 3: 8.92 ms per loop

In [38]: %timeit unique_based(data)
10 loops, best of 3: 29.1 ms per loop
Run Code Online (Sandbox Code Playgroud)


ome*_*rbp 6

一个简单的解决方案可以是:

import numpy as np
def unique_rows(a):
    a = np.ascontiguousarray(a)
    unique_a = np.unique(a.view([('', a.dtype)]*a.shape[1]))
    return unique_a.view(a.dtype).reshape((unique_a.shape[0], a.shape[1]))

data = np.array([[1,8,3,3,4],
                 [1,8,9,9,4],
                 [1,8,3,3,4]])


print unique_rows(data)
#prints:
[[1 8 3 3 4]
 [1 8 9 9 4]]
Run Code Online (Sandbox Code Playgroud)

您可以检查这个对于这个问题有更多的解决方案