【问题标题】:Remove duplicate rows of a numpy array [duplicate]删除numpy数组的重复行[重复]
【发布时间】:2015-09-14 19:52:48
【问题描述】:

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

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

答案应该是这样的:

ans = array([[1,8,3,3,4],
             [1,8,9,9,4]])

如果有两行相同,那么我想删除一个“重复”行。

【问题讨论】:

  • 行在输入数组中最初没有按那个顺序排列是否可以?
  • 是的,顺序不重要
  • 我的问题和你的很相似。 [看这里][1] [1]:stackoverflow.com/questions/31093261/…
  • 我相信现在您可以在轴上应用np.unique,所以np.unique(data, axis = 0) 可以工作。

标签: python numpy


【解决方案1】:

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

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]]

您可以查看this 以获得更多解决此问题的方法

【讨论】:

    【解决方案2】:

    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]
    

    示例运行 -

    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]])
    

    运行时测试 -

    本节对迄今为止提出的解决方案中提出的所有方法进行计时。

    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
    

    【讨论】:

      【解决方案3】:

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

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

      只需将np.unique 应用到data 数组将导致:

      >>> uniques
      array([1, 3, 4, 8, 9])
      

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

      new_array = [tuple(row) for row in data]
      uniques = np.unique(new_array)
      

      哪个打印:

      >>> uniques
      array([[1, 8, 3, 3, 4],
             [1, 8, 9, 9, 4]])
      

      更新

      新版本需要设置np.unique(data, axis=0)

      【讨论】:

      • 是的,它更简单...+1...
      • 我试过new_array = [tuple(row) for row in data] uniques = np.unique(new_array),但它仍然输出独特的array([1, 3, 4, 8, 9])@ThePredator
      • 这是代码,我使用的代码与您的节目相同:import numpy as np data = np.array([[1,8,3,3,4], [1,8,9,9,4], [1,8,3,3,4]]) new_array = [tuple(row) for row in data] uniques = np.unique(new_array) uniques Out[30]: array([1, 3, 4, 8, 9]) 那是关于 numpy 版本的吗?我的 numpy 版本是 1.9.2
      • 我认为以下是正确答案stackoverflow.com/questions/16970982/…
      • 新版本需要设置np.unique(data, axis=0)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 2011-11-18
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多