【问题标题】:Filtering unique valued rows of a numpy array as much as possible尽可能多地过滤numpy数组的唯一值行
【发布时间】:2022-09-24 01:51:55
【问题描述】:

我有一张这样的桌子;

table = np.array([[ 67, 15],
                  [ 90, 15],
                  [ 92, 15],
                  [ 67, 25],
                  [138, 25],
                  [138, 35],
                  [ 62, 15],
                  [ 70, 25],
                  [ 71, 25],
                  [124, 35]])

如果可能的话,我想选择包含唯一值(以前从未显示过)的预定行数(目标)。如果不是,则按照相同的逻辑以相同的顺序选择。

即,如果我想选择;

2 rows: [67,15], [138,25]
3 rows: [67,15], [138,25], [124,35] 
4 rows: [67,15], [138,25], [124,35] , [90,15]
5 rows: [67,15], [138,25], [124,35] , [90,15] , [67,25]

等等。

这是我的审判;

space = []
id_space = [0]
space.append(table[0,:])
target  = 3
row = 1

for i in range(1,len(table)):
    if not (any(np.isin(table[i,:],np.hstack(space)))):
        space.append(table[i,:])
        id_space.extend([i])
        row =  row + 1

    if (row==target):
        break

table[id_space]

它一直有效,直到 target = 3,但其余部分无效。顺便说一句,名为 table 的矩阵在现实中真的很大。也许还可以使用一些更快的替代方案。

提前致谢!

    标签: python numpy filter


    【解决方案1】:

    如果我正确理解了您的要求,则以下代码应为您提供您正在寻找的预期答案。这是一个快速粗略的尝试,并且肯定可以提高效率,因为有很多潜在的重复计算和部分代码可以从一些重构中受益,也许以牺牲简单性为代价。查看它如何处理您的表大小以及是否需要基准以查找代码的较慢部分。

    target  = 5
    # This is a list of the row numbers in the order that they satisfy the logic 
    row_indexes = []
    # This array keeps track of values that we use to filter the table by
    unique_values = np.array([])
    
    while len(row_indexes) != target:
        
        # If there are no unique values to filter the table by we iterate 
        # through the table and choose the first row number that has not already 
        # been added to row_indexes, and the values in that row are used to filter
        # the rest of the table
        if unique_values.size == 0:
    
            for i in range(len(table)):
    
                if i not in row_indexes:
                    row_indexes.append(i)
                    unique_values = np.append(unique_values, table[i])
                    break
    
        else:
            
            # Finds indexes of the rows in table where the entire row is unique using 
            # the unique_values array as the filter 
            bool_arr = np.all(np.isin(table, unique_values, invert=True), axis=1)
            unique_row_indexes = np.where(bool_arr)[0] # np.where returns a tuple of len 1 
    
            # If there are no more unique rows we reset the unique values and loop through again
            if unique_row_indexes.size == 0:
                unique_values = np.array([])
                continue
    
            else:
                
                exists_unique_row = False
    
                for row_index in unique_row_indexes: 
                    
                    # If the unique row has already been added skip it
                    if row_index in row_indexes:
                        continue
                     
                    row_indexes.append(row_index)
                    unique_values = np.append(unique_values, table[row_index])
                    exists_unique_row = True
                    break
                
                # If all unique rows have already been added in a previous pass through
                # we reset the unique values and start again
                if not exists_unique_row:
                    unique_values = np.array([])
    
    
    print(table[row_indexes])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2014-11-27
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 2021-06-08
      • 2019-07-07
      相关资源
      最近更新 更多