How to detect outliers in an ArrayList

后端 未结 8 930
旧时难觅i
旧时难觅i 2021-01-14 02:54

I\'m trying to think of some code that will allow me to search through my ArrayList and detect any values outside the common range of \"good values.\"

Example: 100 1

相关标签:
8条回答
  • 2021-01-14 03:56

    An implementation of the Grubb's test can be found at MathUtil.java. It will find a single outlier, of which you can remove from your list and repeat until you've removed all outliers.

    Depends on commons-math, so if you're using Gradle:

    dependencies {
      compile 'org.apache.commons:commons-math:2.2'
    }
    
    0 讨论(0)
  • 2021-01-14 03:56

    It is just a very simple implementation which fetches the information which numbers are not in the range:

    List<Integer> notInRangeNumbers = new ArrayList<Integer>();
    for (Integer number : numbers) {
        if (!isInRange(number)) {
            // call with a predefined factor value, here example value = 5
            notInRangeNumbers.add(number, 5);
        }
    }
    

    Additionally inside the isInRange method you have to define what do you mean by 'good values'. Below you will find an examplary implementation.

    private boolean isInRange(Integer number, int aroundFactor) {
       //TODO the implementation of the 'in range condition'
       // here the example implementation
       return number <= 100 + aroundFactor && number >= 100 - aroundFactor;
    }
    
    0 讨论(0)
提交回复
热议问题