Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find first local maximum for every group?

Suppose I have a Pandas data frame as follows:

Test Parameter Value

X1     0        0.033285423511615113
X1     1        0.78790279861666179
X1     2        0.79136989638378297
X1     3        0.80063190842016707
X1     4        0.7884653622402551
X1     5        0.78561849214309198...
...
X1     22       22: 0.82241991278171311...
...
X2 ...

I'd like to get the row with Parameter value 3. That is the row with the last increasing value before the first drop. Notice that later on we might have higher values (eg row 22). Essentially, I'm trying to get the "last" number before the "first" decrease value.

Also note that there are multiple Tests, so I probably need to do something like:

myDF.groupby("Test").Something
like image 271
user1357015 Avatar asked Oct 22 '17 05:10

user1357015


People also ask

How do you find the local maxima or peaks in a numeric series?

Approach: The idea is to iterate over the given array arr[] and check if each element of the array is smallest or greatest among their adjacent element. If it is smallest then it is local minima and if it is greatest then it is local maxima.

What is local maxima in DAA?

Local maxima are the highest points organizations reach in their designs when they're data-driven rather than data-informed.

How do you remove local maximums from an array?

Select the max number in the array and delete that number including all the numbers to its right side in the array. Repeat the step 1 for the left elements of the array i.e select the maximum element in the left elements and delete it including all numbers to its right.

How do you find maxima and minima in R?

In R, we can find the minimum or maximum value of a vector or data frame. We use the min() and max() function to find minimum and maximum value respectively. The min() function returns the minimum value of a vector or data frame. The max() function returns the maximum value of a vector or data frame.


1 Answers

Coldspeed nearly has it, to get only the first group you can use cumprod, or similar e.g.

In [11]: df[((df.Value.diff().fillna(1) > 0).cumprod()) == 1].tail(1)
Out[11]:
  Test  Parameter     Value
3   X1          3  0.800632

The trick being:

In [12]: (df.Value.diff().fillna(1) > 0)
Out[12]:
0     True
1     True
2     True
3     True
4    False
5    False
6     True
Name: Value, dtype: bool

In [13]: (df.Value.diff().fillna(1) > 0).cumprod()
Out[13]:
0    1
1    1
2    1
3    1
4    0
5    0
6    0
Name: Value, dtype: int64

Note: My df is this:

In [21]: df
Out[21]:
  Test  Parameter     Value
0   X1          0  0.033285
1   X1          1  0.787903
2   X1          2  0.791370
3   X1          3  0.800632
4   X1          4  0.788465
5   X1          5  0.785618
6   X1         22  0.822420
like image 160
Andy Hayden Avatar answered Oct 24 '22 16:10

Andy Hayden