简体   繁体   中英

Remove rows of a numpy array based on a specific condition

I have an array of four rows A = array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]]) . In each row there are 4 numbers. How do I remove row#3 and row#4 ? In row#3 and row#4 , 1 and 2 appear more than once respectively.

Is there a faster way to do it for arbitrary number of rows and columns? The main aim is to remove those rows where a non negative number appear more than once.

You can use something like this: first create dictionary of occurrences of each value in the sub arrays using np.unique and only keep arrays where no positive number appears more than once.

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)

this returns:

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

My fully-vectorized approach:

  • sort each row
  • detect duplicates by shifting the sorted array to the left by one and compare with itself
  • mark rows with positive duplicates
  • drop
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]])

I modified also to store the indices:

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)

Let me kniow if this is right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM