Javascript:从数组中删除异常值? [英] Javascript: remove outlier from an array?
本文介绍了Javascript:从数组中删除异常值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
values = [8160,8160,6160,22684,0,0,60720,1380,1380,57128]
如何删除离群值,如0、57218、60720和22684?
how can I remove outliers like 0, 57218, 60720 and 22684?
有图书馆可以做到这一点吗?
Is there a library which can do this?
推荐答案
这一切取决于您对异常值"的解释.常用方法:
This all depends on your interpretation of what an "outlier" is. A common approach:
- 离群值超出第三四分位数+ 1.5 *的值四分位间距(IQR)
- 低离群值是第一四分位数以下的任何值-1.5 * IQR
这也是 Wolfram的Mathworld 描述的方法.
这很容易包装在一个函数中:)我试图将下面的内容写清楚;确实存在明显的重构机会.请注意,使用这种常见方法,您给定的样本不包含任何异常值.
This is easily wrapped up in a function :) I've tried to write the below clearly; obvious refactoring opportunities do exist. Note that your given sample contains no outlying values using this common approach.
function filterOutliers(someArray) {
// Copy the values, rather than operating on references to existing values
var values = someArray.concat();
// Then sort
values.sort( function(a, b) {
return a - b;
});
/* Then find a generous IQR. This is generous because if (values.length / 4)
* is not an int, then really you should average the two elements on either
* side to find q1.
*/
var q1 = values[Math.floor((values.length / 4))];
// Likewise for q3.
var q3 = values[Math.ceil((values.length * (3 / 4)))];
var iqr = q3 - q1;
// Then find min and max values
var maxValue = q3 + iqr*1.5;
var minValue = q1 - iqr*1.5;
// Then filter anything beyond or beneath these values.
var filteredValues = values.filter(function(x) {
return (x <= maxValue) && (x >= minValue);
});
// Then return
return filteredValues;
}
这篇关于Javascript:从数组中删除异常值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文