繁体   English   中英

获取索引范围内的行的最大值

[英]Get the max value of rows in index range

我面临以下问题:我需要将Matlab代码重写为Pandas。

问题如下:我有身高差数据。 基于滚动窗口,我确定了高度差的移动平均值和标准差。 当一行的高度差异数据大于移动平均值+ 2 * std时,它将被视为“峰值”(我需要确定)。 原因是,峰可以标识出安装点,而该点在数据集中未给出。 到现在为止还挺好。

现在是我无法解决的最困难的部分:彼此之间可能有多个峰。 当一个峰在另一个峰的10个索引内(1个索引/行= 0.25米,因此一个峰在2.5米之内),则需要将这些峰“合并”:仅需要将最大差异最大的峰保持。 如果该峰未被10个索引内的另一个峰包围,则仅将该值保留为最高安装点。

另一个解决方案是将最大的高度差异和索引分配给周围的峰。

我用滚动窗口的idxmax()尝试了一些方法,但是没有用。 然后,我尝试了以下操作,但仍然无法解决。

首先,我尝试将索引转换为列。 然后我过滤了heightdiff_peak == True的数据框,然后计算了下一个索引的差。 并尝试获得to的最大值,其中当前行的差异小于10。但这并不能提供正确的解决方案。

数据框如下所示:

df:
    Location    abs_diff_height heightdiff_peak index   difference_next_index
277 9.00    4.000000    True    277 1.0
278 9.25    5.000000    True    278 74.0
352 27.75   6.900000    True    352 39.0
391 37.50   6.000000    True    391 169.0
560 79.75   6.000000    True    560 1.0
561 80.00   5.900000    True    561 1.0
562 80.25   5.900000    True    562 1.0
563 80.50   8.900000    True    563 1.0
564 80.75   9.900000    True    564 1.0
565 81.00   10.900000   True    565 1.0
566 81.25   13.900000   True    566 1.0

我尝试了以下代码,但是它不起作用。

def get_max_value(df):
    return df.assign(
    max_diff_height = lambda df: np.where(df['difference_next_index']<10,
                                          df['abs_diff_height'].rolling(2).max().shift(1),
                                          df['abs_diff_height'])
    )


我也尝试过类似的方法:

df[['highest_peak']].rolling(20, center=True).apply(lambda s: s.idxmax(), raw=False)

但是,这只会导致NaNs。

Matlab代码是:

%% Snap multiple detections in a row to the highest point of that peak.
% Initialise variables based on first detection value
x=2;
Remember=PeakIndexT(1);                                          
PeakIndex=PeakIndexT(1);
PeakValue=Dataset(PeakIndexT(1));
while x<=length(PeakIndexT)
    if PeakIndexT(x)-Remember>10                        % If there is more then 10 points (2.5 meters) difference between this and previous detection identify this one as a new one
        PeakIndex=[PeakIndex,PeakIndexT(x)];
        PeakValue=[PeakValue,Dataset(PeakIndexT(x))];

    else                                                % Else merge the detections and use the highest absolute value as the detection peak
        if PeakValue(end)<Dataset(PeakIndexT(x))
            PeakValue(end)=Dataset(PeakIndexT(x));
            PeakIndex(end)=PeakIndexT(x);
        end
    end
    Remember=PeakIndexT(x);                             % Store previous value for reference in loop
    x=x+1;
end


我期望的结果是max_value和索引。

df:
    Location    abs_diff_height heightdiff_peak index   difference_next_index  max_value  index_max_value
277 9.00    4.000000    True    277 1.0     5.0 278 
278 9.25    5.000000    True    278 74.0    5.0 278
352 27.75   6.900000    True    352 39.0    6.9     352
391 37.50   6.000000    True    391 169.0   6.0     591
560 79.75   6.000000    True    560 1.0     13.9    566
561 80.00   5.900000    True    561 1.0     13.9    566
562 80.25   5.900000    True    562 1.0     13.9    566
563 80.50   8.900000    True    563 1.0     13.9    566
564 80.75   9.900000    True    564 1.0     13.9    566
565 81.00   10.900000   True    565 1.0     13.9    566
566 81.25   13.900000   True    566 1.0     13.9    566

IIUC,您首先需要groupby

s = df.difference_next_index.shift().gt(10)
df['index_max_value'] = (df.abs_diff_height                          
                           .groupby([s,s.cumsum()])
                           .transform('idxmax')
                         )

得到:

277    278.0
278    278.0
352    352.0
391    391.0
560    566.0
561    566.0
562    566.0
563    566.0
564    566.0
565    566.0
566    566.0
Name: abs_diff_height, dtype: float64

而获得价值就是

df['max_value'] = df.loc[df['index_max_value'],'abs_diff_height']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM