根据特定条件删除numpy数组的行

时间:2019-12-20 15:16:32

标签: python numpy

我有一个由四行A = array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]])组成的数组。每行中有4个数字。如何删除row#3row#4?在row#3row#4中,12分别出现多次。

对于任意数量的行和列,是否有更快的方法?主要目的是删除那些非负数出现多次的行。

3 个答案:

答案 0 :(得分:2)

您可以使用类似的方法:首先使用np.unique在子数组中创建每个值出现的字典,并仅保留不出现正数超过一次的数组。

A = np.array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]])

new_array = []

# loop through each array
for array in A:
    # Get a dictionary of the counts of each value
    unique, counts = np.unique(array, return_counts=True)
    counts = dict(zip(unique, counts))
    # Find the number of occurences of postive numbers
    positive_occurences = [value for key, value in counts.items() if key > 0]
    # Append to new_array if no positive number appears more than once
    if any(y > 1 for y in positive_occurences):
        continue
    else:
        new_array.append(array)

new_array = np.array(new_array)

这将返回:

array([[-1, -1, -1, -1],
       [-1, -1,  1,  2]])

答案 1 :(得分:2)

我的完全矢量化方法:

  • 对每一行进行排序
  • 通过将排序后的数组向左移动一个并与自身进行比较来检测重复项
  • 用正重复标记行
import numpy as np
a = np.array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]])

# sort each row
b = np.sort(a)

# mark positive duplicates
drop = np.any((b[:,1:]>0) & (b[:,1:] == b[:,:-1]), axis=1)

# drop
aa = a[~drop, :]

Output:
array([[-1, -1, -1, -1],
       [-1, -1,  1,  2]])

答案 2 :(得分:0)

我也修改了存储索引:

A = np.array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]])

new_array = []
**indiceStore = np.array([])**

# loop through each array
for array in A:
    # Get a dictionary of the counts of each value
    unique, counts = np.unique(array, return_counts=True)
    counts = dict(zip(unique, counts))
    # Find the number of occurences of postive numbers
    positive_occurences = [value for key, value in counts.items() if key > 0]
    # Append to new_array if no positive number appears more than once
    if any(y > 1 for y in positive_occurences):
        **indiceStore = np.append(indiceStore, int(array))**
        continue
    else:
        new_array.append(array)

new_array = np.array(new_array)

如果这是对的,请让我知道。