gpt4 book ai didi

python - Python 中的异常值校正

转载 作者:行者123 更新时间:2023-12-01 00:32:21 24 4
gpt4 key购买 nike

我有一个数字列表,例如:

[10
20
2
40
50
60
70
80
0
100]

我想通过计算前两个值和后两个值的平均值来替换数字,以防它小于列表的平均值。像这里 2 将被 (10,20,40,50) 的平均值代替,即 30。类似地,如果它在倒数第二次出现时达到 0,现在它没有接下来的两次出现,在这种情况下,它应该取前三次的平均值下一个保持 4 个值的计数,即 (60,70,80,100) 的平均值,即 77.5。任何人都可以用最好的逻辑来指导我吗?最终输出为:

[10
20
30
40
50
60
70
80
77.6
100]

最佳答案

试试这个代码:

def correct_outliers(s, threshold, n_prev=3, n_next=1):
local_s = s.copy()
updated_index = local_s.to_frame().apply(lambda x: correct_outlier(x, local_s, threshold), axis=1)
return local_s

def correct_outlier(x, s, threshold, n_prev=3, n_next=1):
if x.isna()[0] or x[0] < threshold:
lower_index, upper_index = get_fixed_index(x.name, n_prev, n_next)
s[x.name] = s.loc[lower_index:x.name-1].append(s.loc[x.name+1:upper_index]).mean(skipna=True)
return True
return False

解释和注意事项

  1. 我用过pandas.Series处理数据
  2. correct_outliers函数接收 pandas 系列作为输入, threshold值和窗口边界( n_prevn_nex )
  3. correct_outlier函数由 correct_outliers 调用和逐个元素地应用于输入中的系列 series.apply
  4. get_fixed_bounds给定当前元素索引的函数 i以及窗口边界的值考虑您在申请中提出的请求
  5. correct_outlier的核心函数如下:
    如果该系列的当前值小于threshold值,那么该系列的当前值被 average 替换在固定界限定义的区间内计算(不包括null 值和当前值)

示例

给定以下数据系列:

s = pd.Series([10, 20, 2, 40, 50, 60, 70, 80, 0, 100], dtypes='float')
0 10.0
1 20.0
2 2.0
3 40.0
4 50.0
5 60.0
6 70.0
7 80.0
8 0.0
9 100.0

定义阈值和窗口:

threshold = 5   # s.mean(skipna=True) in your example
n_prev = 3 # 3 element before the current
n_next = 1 # 1 element after the current

现在调用正确的异常值:

fixed_series = correct_outliers(s, n_prev, n_next, threshold), axis=1)

并给出:

0   10.0
1 20.0
2 30.0
3 40.0
4 50.0
5 60.0
6 70.0
7 80.0
8 77.5
9 100.0

逐步执行:

给定与前面示例相同的输入,我将向您展示 x = 2 的逐步执行过程。 ,正如你问我的。

correct_outliers之后调用时,使用 apply 函数迭代该系列,并且在每个元素处 correct_outlier如果正在检查的元素是x = 2,则应用函数,逐步执行如下:

--- correct_outlier(), input: x: 2.0 threshold:5.0  n_prev: 3 n_next: 1
step:
if_condition: x is nan or x<threshold? True
--- get_fixed_index(), input: current_index: 2 n_prev: 3 n_next: 1
step: if_condition: current_index-n_prev>=0? False
output: lower_index: 0 upper_index: 4
slice of series: [10. 20. 40. 50.] mean: 30.0
@@@@ replace the value 2.0 with 30
<小时/>

额外

correct_outliercorrect_outliers功能不是特别高效,主要有以下原因:

  1. 整个系列都是用纯 Python 迭代的,这从来都不是一个好的选择想法。如果可能的话,您应该始终使用库函数来获取数据分析(例如 Pandas、Numpy 等),它们是用 C/C++ 实现的,因此比纯 Python 实现的效率高几个数量级。
  2. 我们可以不使用 correct_outliers 中使用的两个系列之间的附加函数。 ,我们可以简单地通过加权平均来解决问题(这显然要快得多)

第一点是真正的瓶颈。

如何解决?

下面我针对我们所看到的功能提出两种优化方案:

def correct_outliers_opt(s, threshold, n_prev=3, n_next=1):
tmp_s = s.copy()
tmp_s[tmp_s < threshold].to_frame().apply(lambda x: correct_outlier4(x, tmp_s, threshold), axis=1)
return tmp_s

def correct_outlier_opt(x, s, threshold, n_prev=3, n_next=1):
i = x.name
lower_index, upper_index = get_fixed_index(x.name, n_prev, n_next)
n = upper_index - lower_index
mean = s.loc[lower_index:i-1].mean(skipna=True)*(i-lower_index)/n + ss.loc[i+1:upper_index].mean(skipna=True)*(upper_index-i)/n
s[i] = mean
return mean

关键点在correct_outliers_opt内如下:

tmp_s[tmp_s < threshold]

通过这种方式,我在迭代之前过滤了该系列(利用 Pandas 函数而不是纯 python):这样只有满足条件的值才会被迭代。在我们的示例中,我们在 python 中仅迭代需要替换的 2 个值,而不是迭代整个系列。

第二个优化的事情(它对性能的影响比前一点小得多)是函数correct_outlier_opt内平均值的计算:现在不再在系列之间进行追加,而是分别计算两个系列的平均值,然后进行加权平均以获得单个结果。

执行时间比较

两者都接收相同的输入并返回相同的输出,但执行时间明显不同。

执行时间是根据以下基准实例计算的:

threshold = 5
n_prev, n_next = 3, 1
N = 1000

ss = pd.Series([10, 20, 2, 40, 50, 60, 70, 80, 0, 100] * N, dtype='float') # total len N * 10

正确的异常值:

%%timeit
correct_outliers(ss, threshold)
# Execution time: 2.95 s ± 417 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

优化版本:

%%timeit
correct_outliers_opt(ss, threshold)
#Execution time: 545 ms ± 16.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

如您所见,优化后的版本速度提高了约 6 倍。

关于python - Python 中的异常值校正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58087282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com