1

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.

3 Answers 3

2

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]])
Sign up to request clarification or add additional context in comments.

2 Comments

is there a way to know the indices of the rows which are not considered?
i modifed a bit. Please see my answer and let me know what i did is ok.
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]])

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.