2010-11-16 72 views

回答

61

是的,似乎確實有第三個庫(在Java數學中沒有)。兩個已經拿出有:

http://opsresearch.com/app/

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

,但它實際上並不難寫自己的方法來計算平均數,中位數,模式和範圍。

MEAN

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; 
    } 
} 

MODE

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指出的那樣,上述不適用於多模式集合。我們可以解決這個問題很容易:如果您使用的是Java 8或更高

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; 
} 

加成

,你也可以判斷這樣的模式:

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()); 
} 
+0

謝謝,但我更願意使用一些現成的,如果可能的 – user339108 2010-11-16 06:47:57

+0

@Stephen C.我們對此深感抱歉,我再次更新的鏈接。 – 2010-11-16 06:57:09

+0

如果您有一個非常大的數組或者必須實時計算值,那麼這個類將會有問題。它可以寫成沒有數組的平均值和標準偏差;沒有中位數和模式的確定。 – duffymo 2010-11-16 10:30:16

2

MODE算法不考慮具有多種模式(雙峯,三峯,...)的情況 - 當有多個數字出現在與maxCount相同的次數時,會發生這種情況。考慮到這一點,它應該返回一個數組而不是一個int值。

-1
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; 
    } 
} 

測試數據

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

0
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; 
    }