如何从一组数字中计算平均数,中位数,模式和范围

是否有任何函数(作为math库的一部分),将计算从一组数字的平均值 ,中位数,模式和范围。

是的,似乎有第三个库(Javamath中没有)。 有两个是:

http://opsresearch.com/app/

http://www.iro.umontreal.ca/~simardr/ssj/indexe.html

但是,编写自己的方法来计算平均值,中位数,模式和范围其实并不困难。

意思

public static double mean(double[] m) { double sum = 0; for (int i = 0; i < m.length; i++) { sum += m[i]; } return sum / m.length; } 

MEDIAN

 // the array double[] m MUST BE SORTED public static double median(double[] m) { int middle = m.length/2; if (m.length%2 == 1) { return m[middle]; } else { return (m[middle-1] + m[middle]) / 2.0; } } 

模式

 public static int mode(int a[]) { int maxValue, maxCount; for (int i = 0; i < a.length; ++i) { int count = 0; for (int j = 0; j < a.length; ++j) { if (a[j] == a[i]) ++count; } if (count > maxCount) { maxCount = count; maxValue = a[i]; } } return maxValue; } 

UPDATE

正如Neelesh Salpe所指出的那样,上述不适合多式联运。 我们可以很容易解决这个问题:

 public static List<Integer> mode(final int[] numbers) { final List<Integer> modes = new ArrayList<Integer>(); final Map<Integer, Integer> countMap = new HashMap<Integer, Integer>(); int max = -1; for (final int n : numbers) { int count = 0; if (countMap.containsKey(n)) { count = countMap.get(n) + 1; } else { count = 1; } countMap.put(n, count); if (count > max) { max = count; } } for (final Map.Entry<Integer, Integer> tuple : countMap.entrySet()) { if (tuple.getValue() == max) { modes.add(tuple.getKey()); } } return modes; } 

加成

如果您使用的是Java 8或更高版本,则还可以确定如下所示的模式:

 public static List<Integer> getModes(final List<Integer> numbers) { final Map<Integer, Long> countFrequencies = numbers.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); final long maxFrequency = countFrequencies.values().stream() .mapToLong(count -> count) .max().orElse(-1); return countFrequencies.entrySet().stream() .filter(tuple -> tuple.getValue() == maxFrequency) .map(Map.Entry::getKey) .collect(Collectors.toList()); } 

从apache检查公用math 。 那里有很多。

MODEalgorithm不考虑多个模式(双峰,三峰,…)的情况 – 当有多个数字出现在与maxCount相同的次数时,会发生这种情况。 考虑到这一点,它应该返回一个数组而不是一个int值。

  public static Set<Double> getMode(double[] data) { if (data.length == 0) { return new TreeSet<>(); } TreeMap<Double, Integer> map = new TreeMap<>(); //Map Keys are array values and Map Values are how many times each key appears in the array for (int index = 0; index != data.length; ++index) { double value = data[index]; if (!map.containsKey(value)) { map.put(value, 1); //first time, put one } else { map.put(value, map.get(value) + 1); //seen it again increment count } } Set<Double> modes = new TreeSet<>(); //result set of modes, min to max sorted int maxCount = 1; Iterator<Integer> modeApperance = map.values().iterator(); while (modeApperance.hasNext()) { maxCount = Math.max(maxCount, modeApperance.next()); //go through all the value counts } for (double key : map.keySet()) { if (map.get(key) == maxCount) { //if this key's value is max modes.add(key); //get it } } return modes; } //std dev function for good measure public static double getStandardDeviation(double[] data) { final double mean = getMean(data); double sum = 0; for (int index = 0; index != data.length; ++index) { sum += Math.pow(Math.abs(mean - data[index]), 2); } return Math.sqrt(sum / data.length); } public static double getMean(double[] data) { if (data.length == 0) { return 0; } double sum = 0.0; for (int index = 0; index != data.length; ++index) { sum += data[index]; } return sum / data.length; } //by creating a copy array and sorting it, this function can take any data. public static double getMedian(double[] data) { double[] copy = Arrays.copyOf(data, data.length); Arrays.sort(copy); return (copy.length % 2 != 0) ? copy[copy.length / 2] : (copy[copy.length / 2] + copy[(copy.length / 2) - 1]) / 2; } 
 public class Mode { public static void main(String[] args) { int[] unsortedArr = new int[] { 3, 1, 5, 2, 4, 1, 3, 4, 3, 2, 1, 3, 4, 1 ,-1,-1,-1,-1,-1}; Map<Integer, Integer> countMap = new HashMap<Integer, Integer>(); for (int i = 0; i < unsortedArr.length; i++) { Integer value = countMap.get(unsortedArr[i]); if (value == null) { countMap.put(unsortedArr[i], 0); } else { int intval = value.intValue(); intval++; countMap.put(unsortedArr[i], intval); } } System.out.println(countMap.toString()); int max = getMaxFreq(countMap.values()); List<Integer> modes = new ArrayList<Integer>(); for (Entry<Integer, Integer> entry : countMap.entrySet()) { int value = entry.getValue(); if (value == max) modes.add(entry.getKey()); } System.out.println(modes); } public static int getMaxFreq(Collection<Integer> valueSet) { int max = 0; boolean setFirstTime = false; for (Iterator iterator = valueSet.iterator(); iterator.hasNext();) { Integer integer = (Integer) iterator.next(); if (!setFirstTime) { max = integer; setFirstTime = true; } if (max < integer) { max = integer; } } return max; } } 

testing数据

{3,1,5,2,4,1,3,4,3,2,1,3,4,1}的模式{1,3};
{3,1,5,2,4,1,3,4,3,2,1,3,4,1,-1,-1,-1,-1,-1}的模式{-1} ;