How to replace only the first n elements in a numpy array that are larger than a certain value?

Cleb

I have an array myA like this:

array([ 7,  4,  5,  8,  3, 10])

If I want to replace all values that are larger than a value val by 0, I can simply do:

myA[myA > val] = 0

which gives me the desired output (for val = 5):

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

However, my goal is to replace not all but only the first n elements of this array that are larger than a value val.

So, if n = 2 my desired outcome would look like this (10 is the third element and should therefore not been replaced):

array([ 0,  4,  5,  0,  3, 10])

A straightforward implementation would be:

import numpy as np

myA = np.array([7, 4, 5, 8, 3, 10])
n = 2
val = 5

# track the number of replacements
repl = 0

for ind, vali in enumerate(myA):

    if vali > val:

        myA[ind] = 0
        repl += 1

        if repl == n:
            break

That works but maybe someone can can up with a smart way of masking!?

JuniorCompressor

The following should work:

myA[(myA > val).nonzero()[0][:2]] = 0

since nonzero will return the indexes where the boolean array myA > val is non zero e.g. True.

For example:

In [1]: myA = array([ 7,  4,  5,  8,  3, 10])

In [2]: myA[(myA > 5).nonzero()[0][:2]] = 0

In [3]: myA
Out[3]: array([ 0,  4,  5,  0,  3, 10])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find if any array elements are larger than a certain value using `arrayfun`

how to check if a value inside an array is larger than a certain value

Replace all elements of Python NumPy Array that are greater than some value

Aggregate the elements of an array to not be larger than a defined value

Python: numpy array larger and smaller than a value

Replace elements of numpy array based on first occurrence of a particular value

find first and last element of a NumPy array larger than a threshold

How to print only elements that are larger than the previous ones in 2D array in C

Retrieve value larger than a threshold value from numpy array optimally

How to find the first value in a column larger than the previous value in the column

how to select only objects that contain an array with more than N elements

How do I find the 3d indices of where one array is larger than another in R (in order to replace these elements with NaN)?

Replace elements in Numpy array by value and location

PostgreSQL: Replace values greater than a certain limit in array elements

Is there a way to find the sum of the last n and first n elements in a 2D array using only NumPy?

First n elements of row in numpy array

How to apply rules to only certain elements in array

How do I efficiently count elements less than a certain value in a sorted array?

How to find and replace specific elements in numpy array?

How to replace all the elements of a numpy array?

How to find which array key has value larger than 0

How can I set the value of certain elements in a 2D array having their positions in a list with numpy?

Replace elements in numpy array with closest value in another array

Insert elements in between in numpy array after a certain value

Python - How to replace boolean value if less than a certain number?

Return only elements of an array in an object that contain a certain value

How to replace Nan value with zeros in a numpy array?

How to replace one column by a value in a numpy array?

Numpy: How to replace array with single value with vectorization?