- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数字列表,例如:
[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
pandas.Series
处理数据correct_outliers
函数接收 pandas 系列作为输入, threshold
值和窗口边界( n_prev
和 n_nex
)correct_outlier
函数由 correct_outliers
调用和逐个元素地应用于输入中的系列 series.apply
get_fixed_bounds
给定当前元素索引的函数 i
以及窗口边界的值考虑您在申请中提出的请求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_outlier
和correct_outliers
功能不是特别高效,主要有以下原因:
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/
我试图通过叠加更新的(更详细的)卫星图像(我从 {leaflet} 包中获取)来改善 Rayshader 的外观,但叠加层不匹配与 3D 渲染。 理想情况下,我正在寻找可以获取全局卫星图像的开源解决方
我想构建一个由单个数字组成的常量数组(1..200)来制作一个“查找表”,以将值为 1 - 200 的滚动条的位置转换为用于对图像应用 Gamma 校正的值。 第一个数组值的值在 7.0 - 9.9
我尝试对图像进行一些简单的 Gamma 校正。起初,我尝试使用 Matlab,然后将其应用于 opencv。但我得到不同的结果。下面是部分代码。代码哪里出错了? 在matlab中: for i=1:r
我使用 DirectXTex 库捕获 DX11 游戏的屏幕截图并将其保存到文件中。问题是当我将它保存为 jpeg 时效果很好,但如果我将它保存为 png 图像会变得非常明亮并被洗掉。我使用 Tweak
我正在尝试使用以下代码检测图像中ID卡的边界。关键是我使用的 Gamma 值。我使用2或3的值(假设我希望卡在背景下突出显示)。使用背景较浅或与卡片颜色本身一样浅的照片时遇到问题。请看下面的图片..第
我正在进行立体视觉设置,机翼上方安装了 2 个摄像头。左摄像头向内倾斜几度,而右摄像头与机翼平行。所有可用图像 here 然后使用(剪切和粘贴,但不按原样编译) // performing stere
我正在尝试解决有关 NFA 的问题。指令如下:字母{a, b, c}。 • L1 是最后一个字符与倒数第五个字符相同的所有字符串。例如,应该接受字符串 aaacbacbca,因为倒数第五个字符和最后一
我尝试使用 qt 更改图像的 Gamma,但没有得到理想的结果。这是我的代码: QImage Filters::aply_filtre_gamma(QImage image){ // (std:
我需要对 Y'CbCr 空间中的图像进行 Gamma 校正,以便在图像中的饱和和饥饿区域中显示细节,我想知道是否需要调整色度子 channel ? 我知道如果我让亮度 channel 像素挨饿,如果我
我不了解opencv中hog.cpp中的 Gamma 校正代码,我经历了一些链接here与opencv hog.cpp中的代码不匹配 Mat_ _lut(1, 256); const float* l
根据本页http://www.w3schools.com/cssref/css3_pr_filter.asp有对比度、亮度、色调、饱和度等。但没有明确访问 Gamma 。有没有办法用现有的 CSS3
我进行了超几何分析(使用 Python 脚本)来研究 GO-terms 在基因子集中的富集。我的输出示例如下: GO00001 1500 300 200 150 5.39198144708e-7
我使用 Opencv 编写了一个 Android 应用程序,我的图像处理算法需要对检测到的矩形进行正确的旋转,因此作为该过程的开始,我 将最大的矩形检测为 RotatedRect。 获取矩形的旋转角度
我正在使用 OpenCV 校准和校正立体声系统。我有一个眼睛会聚的立体相机,实际上我按以下顺序运行这些功能: for(int j=0; j < ChessBoard.numSquares; j++)
我会对图像使用 Gamma 校正。因此,我必须使用 G = 0.6 为源图像的每个像素强度赋值。我有问题,因为目标图像完全错误。当我从源图像中获取像素时,可能会遇到转换问题。这是我的代码: #incl
我正在构建一个 Android 应用程序,为用户提供一些图像处理功能。但在应用任何图像转换功能之前,我想进行 Gamma 校正以改善图像。我知道如何执行 Gamma 校正,但我不知道要使用什么 Gam
我在 Windows 10 上使用 SDL2 创建 OpenGL 上下文,但是当我尝试在 Intel UHD 630 上获取帧缓冲区附件颜色编码时,我收到了无效操作错误。在我的 Nvidia Gefo
我有RGB数据和Gamma校正比例 我可以用下面的来计算吗 R = pow(R, 1/Gamma) G = pow(G, 1/Gamma) B = pow(B, 1/Gamma) 或 Gamma 校正
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
自 Snow Leopard 以来,QTKit 现在从 QTMovies frameImageAtTime:withAttributes:error: 等函数返回颜色校正后的图像数据。给定未压缩的 A
我是一名优秀的程序员,十分优秀!