Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a fast way to compare one element in a numpy array to the rest of the elements in that array?

I have an array, and I want to see if any element in that array is greater than or equal to any other element in that array. I could do two for loops, but my array has a length of 10,000 or greater, and so that created a very slow program. Anyway I can do this faster?

[EDIT] I only need it to see if it's greater than or equal to the elements that come after the element I am looking at, and if it is, I need to know it's index.

[EDIT] I am going to re-explain my problem more clearly, because the current solutions aren't working for what I am needing. To start off, here is some code

x=linspace(-10, 10, 10000)
t=linspace(0,5,10000)

u=np.exp(-x**2)

k=u*t+x

So I take an x array, get the height of that by putting it into the Gaussian, then based on that height, that is the speed at which that x value is propagating through space, which I find with k. My problem is, I need to find when the Gaussian becomes a double-valued function (or in other words, when a shock happens). If I do argmax solution, I will always get the last value in k because it is very close to zero, I need the first value after the element that will give me a double value in my function.

[Edit] Small Example

x=[0,1,2,3,4,5,6,7,8,9,10] #Input 
k=[0,1,2,3,4,5,6,5,4,10] #adjusted for speed

output I want
in this case, 5 is the first number that goes above a number that comes after it.
So I need to know the index of where 5 is located and possibly the index 
of the number that it is greater than
like image 407
Wesley Bowman Avatar asked Mar 11 '13 14:03

Wesley Bowman


People also ask

How do you compare each element in a NumPy array?

To compare each element of a NumPy array arr against the scalar x using any of the greater (>), greater equal (>=), smaller (<), smaller equal (<=), or equal (==) operators, use the broadcasting feature with the array as one operand and the scalar as another operand.

How do I compare values in two NumPy arrays?

Method 1: We generally use the == operator to compare two NumPy arrays to generate a new array object. Call ndarray. all() with the new array object as ndarray to return True if the two NumPy arrays are equivalent.

How do you compare each value in an array?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.


2 Answers

The first value that is greater than a later value necessarily corresponds to the minimum among local minima:

k = np.array([0,1,2,3,4,5,6,5,4,10])
lm_i = np.where(np.diff(np.sign(np.diff(k))) > 0)[0] + 1
mlm = np.min(k[lm_i])
mlm_i = lm_i[np.argmin(k[lm_i])]

The index of the first value greater than a later value is then the first index greater than that minimum local minimum:

i = np.where(k > mlm)[0][0]

Plot of solution

(Ignore that the graph appears to cross the horizontal line at the tangent; that's just a display artefact.)

As a one-liner:

np.where(k > np.min(k[np.where(np.diff(np.sign(np.diff(k))) > 0)[0] + 1]))[0][0]

Note that this is approx. 1000 times faster than root's solutions, as it is entirely vectorised:

%timeit np.where(k > np.min(k[np.where(np.diff(np.sign(np.diff(k))) > 0)[0] + 1]))[0][0]
1000 loops, best of 3: 228 us per loop
like image 104
ecatmur Avatar answered Oct 09 '22 23:10

ecatmur


Vectorised solution, that is about 25% faster than ecatmur's:

np.where(k > np.min(k[np.where(np.diff(k) < 0)[0][0]:]))[0][0]

A naive approach:

next(i for i in np.arange(len(arr)) if arr[i:].argmin() != 0)
like image 3
root Avatar answered Oct 10 '22 01:10

root