使用Java 8流计算加权平均值

15
如何计算Map<Double, Integer>的加权平均值,其中整数值是对要平均的双精度值的权重。例如:地图具有以下元素:
  1. (0.7, 100) // 值为0.7,权重为100
  2. (0.5, 200)
  3. (0.3, 300)
  4. (0.0, 400)
我想使用Java 8流应用以下公式,但不确定如何同时计算分子和分母并保留它。如何在此处使用reduction?

enter image description here


4
你有查看过Collectors.averagingDouble方法吗? - Aaron
4个回答

22
你可以为此任务创建自己的收集器:
static <T> Collector<T,?,Double> averagingWeighted(ToDoubleFunction<T> valueFunction, ToIntFunction<T> weightFunction) {
    class Box {
        double num = 0;
        long denom = 0;
    }
    return Collector.of(
             Box::new,
             (b, e) -> { 
                 b.num += valueFunction.applyAsDouble(e) * weightFunction.applyAsInt(e); 
                 b.denom += weightFunction.applyAsInt(e);
             },
             (b1, b2) -> { b1.num += b2.num; b1.denom += b2.denom; return b1; },
             b -> b.num / b.denom
           );
}

这个自定义收集器以两个函数作为参数: 第一个函数返回给定流元素的值 (作为 ToDoubleFunction), 第二个函数返回权重 (作为 ToIntFunction)。它使用辅助本地类在收集过程中存储分子和分母。每次接受一个条目时,分子会增加值与权重的乘积结果,并且分母会增加权重。最后,完成者将两者相除后返回一个 Double

一个示例用法如下:

Map<Double,Integer> map = new HashMap<>();
map.put(0.7, 100);
map.put(0.5, 200);

double weightedAverage =
  map.entrySet().stream().collect(averagingWeighted(Map.Entry::getKey, Map.Entry::getValue));

非常感谢您的解释,让我受益匪浅。我会进一步了解自定义收集器的知识。 - Vivek Sethi

3
您可以使用此过程来计算地图的加权平均值。请注意,地图条目的键应包含该值,并且地图条目的值应包含权重。
     /**
     * Calculates the weighted average of a map.
     *
     * @throws ArithmeticException If divide by zero happens
     * @param map A map of values and weights
     * @return The weighted average of the map
     */
    static Double calculateWeightedAverage(Map<Double, Integer> map) throws ArithmeticException {
        double num = 0;
        double denom = 0;
        for (Map.Entry<Double, Integer> entry : map.entrySet()) {
            num += entry.getKey() * entry.getValue();
            denom += entry.getValue();
        }

        return num / denom;
    }

您可以查看其单元测试以了解用例。

     /**
     * Tests our method to calculate the weighted average.
     */
    @Test
    public void testAveragingWeighted() {
        Map<Double, Integer> map = new HashMap<>();
        map.put(0.7, 100);
        map.put(0.5, 200);
        Double weightedAverage = calculateWeightedAverage(map);
        Assert.assertTrue(weightedAverage.equals(0.5666666666666667));
    }

你需要这些导入来进行单元测试:

import org.junit.Assert;
import org.junit.Test;

您需要这些导入代码:

import java.util.HashMap;
import java.util.Map;

我希望能对您有所帮助。

1
public static double weightedAvg(Collection<Map.Entry<? extends Number, ? extends Number> data) {
    var sumWeights = data.stream()
        .map(Map.Entry::getKey)
        .mapToDouble(Number::doubleValue)
        .sum();
    var sumData = data.stream()
        .mapToDouble(e -> e.getKey().doubleValue() * e.getValue().doubleValue())
        .sum();
    return sumData / sumWeights;
}

0
static float weightedMean(List<Double> value, List<Integer> weighted, int n) {
    int sum = 0;
    double numWeight = 0;

    for (int i = 0; i < n; i++) {
        numWeight = numWeight + value.get(i).doubleValue() * weighted.get(i).intValue();
        sum = sum + weighted.get(i).intValue();
    }

    return (float) (numWeight) / sum;
}

1
当回答一个旧问题时,如果您包含一些上下文来解释您的答案如何帮助,特别是对于已经有被接受答案的问题,那么您的答案将对其他StackOverflow用户更有用。请参阅:如何撰写好的答案 - David Buck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接