1

I have a array of [2 2 3 4 4 5 6 6 6], and want to delete all minimum values from it.

The output should be [3 4 4 5 6 6 6].

I tried like following code, but it deleted a single 2, and left [2 3 4 4 5 6 6 6].

import numpy as np

a = np.array([2,2,3,4,4,5,6,6,6])
b= np.delete(a, a.argmin())
0

4 Answers 4

2

Python has a built-in function for finding min

val = min(values)
b =[v for v in values if v != val]

Sign up to request clarification or add additional context in comments.

1 Comment

It works fine. Try to find a native numpy function to avoid looping whenever possible, so you take advantage of numpy's C implementation for better performance.
0
b = a[a > min(a)]

[b]:
array([3, 4, 4, 5, 6, 6, 6])

Comments

0

The size of numpy arrays is immutable but you can create a copy of this array using this simple oneliner:

arr = np.array([2, 2, 3, 4, 4, 5, 6, 6, 6])
arr[arr!=np.min(arr)]

Output:

array([3, 4, 4, 5, 6, 6, 6])

Comments

0

You can get the indices that are greater than minimum value and slice the array as in this answer.

out = a[a>a.min()]

Notice this is faster than using np.delete as explained in the linked answer.

Comments

Your Answer

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