0

我有一个数组中的数字列表,我想计算上栅栏。

我知道我必须计算中位数,这可以使用 math.js 库来完成。

var median = math.median(numList);

然后第三个四分位数是中位数的上半部分。我想我必须先排序,我相信这可以通过,

numList.sort(function(a,b){return a - b});

但我不确定如何从这里开始计算第三个四分位和四分位间距以获得上栅栏。

任何帮助深表感谢。

4

1 回答 1

1

您可以继续使用中位数,让Math.js为您进行排序。

function quartileBounds(_sample){
    // find the median as you did
    var _median = math.median(_sample)

    // split the data by the median
    var _firstHalf = _sample.filter(function(f){ return f < _median })
    var _secondHalf = _sample.filter(function(f){ return f >= _median })

    // find the medians for each split
    var _25percent = math.median(_firstHalf);
    var _75percent = math.median(_secondHalf);

    var _50percent = _median;
    var _100percent = math.max(_secondHalf);

    // this will be the upper bounds for each quartile
    return [_25percent, _50percent, _75percent, _100percent];
}

quartileBounds([7,18,33,32,10,30,77,40,135,30,121,36,26,28,60,80,17,288,114]);
// returns [26,33,78.5,288]
于 2016-10-12T16:57:16.337 回答