Skip to content
Advertisement

Set numpy array elements to zero if they are above a specific threshold

Say, I have a numpy array consists of 10 elements, for example:

a = np.array([2, 23, 15, 7, 9, 11, 17, 19, 5, 3])

Now I want to efficiently set all a values higher than 10 to 0, so I’ll get:

[2, 0, 0, 7, 9, 0, 0, 0, 5, 3]

Because I currently use a for loop, which is very slow:

JavaScript

How can I achieve that in the most efficient way, having in mind big arrays of, say, 10^6 elements?

Advertisement

Answer

Generally, list comprehensions are faster than for loops in python (because python knows that it doesn’t need to care for a lot of things that might happen in a regular for loop):

JavaScript

but, as @unutbu correctly pointed out, numpy allows list indexing, and element-wise comparison giving you index lists, so:

JavaScript

would be even faster.

Generally, when applying methods on vectors of data, have a look at numpy.ufuncs, which often perform much better than python functions that you map using any native mechanism.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement