熊猫-查找最大值不计算离群值

时间:2020-07-06 21:54:24

标签: python pandas scipy statistics

我有一个数据框,其中每一列代表一个地理位置,每一行代表一天中的一分钟。每个像元的值是CFS中该点的水流量。下面是这些时间流序列之一的图。

基本上,我需要计算白天每个位置的最大流量的绝对值,在这种情况下,这将是187 cfs的驼峰。但是,存在不稳定因素,因此DF.abs()。max()返回1197 cfs。我需要以某种方式删除计算中的异常值。如您所见,离群值没有规律,但是如果您查看图表,则连续2个时间点的流量变化都不应超过x%。我应该提到其中有15K,所以最快的解决方案是最好的。

任何人都知道如何在python中完成此工作,或者至少知道我要做什么的统计字?谢谢!

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您要查找的统计字 平滑 去噪 < / em>数据。

这是我的尝试:

# Importing packages
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import savgol_filter

# Creating a curve with a local maximum to simulate "ideal data"
x = np.arange(start=-1, stop=1, step=0.001)
y_ideal = 10**-(x**2)

# Adding some randomly distributed outliers to simulate "real data"
y_real = y_ideal.copy()
np.random.seed(0)
for i in range(50):
    x_index = np.random.choice(len(x))
    y_real[x_index] = np.random.randint(-3, 5)

# Denoising with Savitzky-Golay (window size = 501, polynomial order = 3)
y_denoised = savgol_filter(y_real, window_length=501, polyorder=3)
# You should optimize these values to fit your needs

# Getting the index of the maximum value from the "denoised data"
max_index = np.where(y_denoised == np.amax(y_denoised))[0]

# Recovering the maximum value and reporting
max_value = y_real[max_index][0]
print(f'The maximum value is around {max_value:.5f}')

enter image description here

请记住:

  1. 此解决方案是近似

  2. 您应该找到window_length函数中插入的polyordersavgol_filter()参数的最佳参数。

  3. 如果最大值所在的区域嘈杂,则可以使用max_value = y_denoised [max_index][0]代替max_value = y_real[max_index][0]

注意:此解决方案基于另一个堆栈溢出answer