gpt4 book ai didi

arrays - 从 moSTLy 线性递增数组中删除异常值

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:37:15 26 4
gpt4 key购买 nike

给定一个线性递增的数组,但是

  • 可能会遗漏一些数字
  • 可能会有一些意想不到的数字,

您将如何构建算法以从数组中删除所有离群值?

可能的数组示例:

1,2,3,4,1,1,1,100,5,6,7

1,2,4,100,5,6,7

1,2,4,100,101,5,6,7,300

2,3,4,5,6,7,300

在上面的所有示例中,您应该能够知道数组应该是 1-7 或 2-7。

一些现实生活中的示例数组:

1, 2, 295, 296, 297, 4, 5, 6, 8, 9, 10, 11, 12, 13, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 6, 6, 6, 6, 5, 5, 6, 6, 6, 6, 6, 4, 6, 6, 3, 4, 6, 6, 6, 5, 6, 6, 6, 4, 5, 6, 3, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 4, 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 4, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 3, 3, 6, 6, 6, 3, 6, 6, 4, 4, 6, 6, 6, 6, 6, 3, 6, 6, 6, 3, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 3, 6, 6, 6, 6, 6, 6, 5, 6, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 15, 18, 20, 21, 22, 23, 24, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 26, 712, 383, 114, 118, 225, 304, 323, 349, 357, 550, 556, 590, 649, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 51

我想出的一个解决方案是过滤掉所有大于 N(=5?) 大于先前有效值的值,以及所有小于先前有效值的值。

const filterOutliers = (someArray) => {
let previousValidValue = null;
return someArray.filter((x, index) => {
//Assume the first value is valid - although this assumption might not always be true.
if(!previousValidValue) {
previousValidValue = x;
return true;
}
// if the number is less than the previous valid value, remove it
if(x < previousValidValue) {
return false;
}
// if the number is more than 5 greater than the last valid value, remove it
if(x > previousValidValue + 5) {
return false;
}
previousValidValue = x;
return true;
})
}

潜在相关链接:Javascript: remove outlier from an array?

最佳答案

您的异常数据值似乎确实有一个恒定的增量。因此,计算增量,采用模式并保持跟随此增量的值序列(到合适的公差)。

关于arrays - 从 moSTLy 线性递增数组中删除异常值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55791245/

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