是否有任何函数(作为数学库的一部分),它将从一组数字中计算mean,中位数,模式和范围。如何从一组数字计算平均值,中位数,模式和范围
41
A
回答
61
是的,似乎确实有第三个库(在Java数学中没有)。两个已经拿出有:
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());
}
11
检查出commons math from apache。那里有很多。
+0
查看关于Adeel答案的评论:Apache Commons Math似乎使用了非常低效的中值算法。 – Chinasaur 2012-08-24 18:47:46
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;
}
相关问题
- 1. 如何通过范围10来计算数组的平均值?
- 2. 如何计算Java中一组连续数字的平均值,中位数和模式?
- 3. 如何计算数组的平均值?
- 4. 如何计算一个范围内的平均值psql
- 5. 如何计算一个范围的平均值?
- 6. 计算数组中的平均值
- 7. 如何计算范围的前两个最高数字的平均值?
- 8. 如何找到字典键,然后取平均值,中位数和范围
- 9. 如何使用Thrust从int2数组中计算平均值
- 10. 计算2位数的平均值
- 11. MySQL范围和平均值
- 12. 如何使用BEX计算“平均模式”,中值模式?
- 13. 计算数组部分的平均值
- 14. 计算数组的平均值
- 15. 如何计算数字分组的平均值
- 16. 平均值范围
- 17. 数值范围的平均值
- 18. MongoDB:如何计算一个嵌套数组的平均值
- 19. 计算平均中位数模式c编程阵列
- 20. 计算熊猫数据框中的平均真实范围列
- 21. Java,使用Math.random计算中位数和平均值
- 22. 如何计算平均值?
- 23. 如何在单个选择查询中获取平均值,中位数,模式和范围?
- 24. 如何计算用户插入值的数组的平均值?
- 25. 计算平均数,中位数和用户输入的整数
- 26. 在Python中用数据框计算平均值/平均值Pandas
- 27. 从数组中读取并计算平均值
- 28. 如何计算Python中数字的平均值
- 29. 计算数组的平均值和其他度量值
- 30. 从句子中提取数字并计算平均值。
谢谢,但我更愿意使用一些现成的,如果可能的 – user339108 2010-11-16 06:47:57
@Stephen C.我们对此深感抱歉,我再次更新的链接。 – 2010-11-16 06:57:09
如果您有一个非常大的数组或者必须实时计算值,那么这个类将会有问题。它可以写成没有数组的平均值和标准偏差;没有中位数和模式的确定。 – duffymo 2010-11-16 10:30:16