[removed] remove outlier from an array?

前端 未结 5 1637
-上瘾入骨i
-上瘾入骨i 2021-01-02 03:10
values = [8160,8160,6160,22684,0,0,60720,1380,1380,57128]

how can I remove outliers like 0, 57218, 60720 and 22684?

Is there a library whic

5条回答
  •  悲哀的现实
    2021-01-02 03:58

    This all depends on your interpretation of what an "outlier" is. A common approach:

    • High outliers are anything beyond the 3rd quartile + 1.5 * the inter-quartile range (IQR)
    • Low outliers are anything beneath the 1st quartile - 1.5 * IQR

    This is also the approach described by Wolfram's 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;
    }
    

提交回复
热议问题