如何使用Java中的Apache Math 3.0生成直方图的垃圾箱? 我一直在寻找使用Apache Common Math 3.0的特定数据集生成垃圾箱(通过指定下部频段,上限和所需箱数)。我研究了频率http://

问题描述 投票:0回答:5

这里是使用Apache Commons Math 3:实现直方图的简单方法。 final int BIN_COUNT = 20; double[] data = {1.2, 0.2, 0.333, 1.4, 1.5, 1.2, 1.3, 10.4, 1, 2.0}; long[] histogram = new long[BIN_COUNT]; org.apache.commons.math3.random.EmpiricalDistribution distribution = new org.apache.commons.math3.random.EmpiricalDistribution(BIN_COUNT); distribution.load(data); int k = 0; for(org.apache.commons.math3.stat.descriptive.SummaryStatistics stats: distribution.getBinStats()) { histogram[k++] = stats.getN(); }

java statistics histogram bins apache-commons-math
5个回答
17
投票

据我所知,Apache Commons中没有良好的直方图类。 我最终写了自己的书。 如果您想要的只是从min到max线性分布的垃圾箱,那么编写非常容易。

可能是这样的:
public static int[] calcHistogram(double[] data, double min, double max, int numBins) {
  final int[] result = new int[numBins];
  final double binSize = (max - min)/numBins;

  for (double d : data) {
    int bin = (int) ((d - min) / binSize);
    if (bin < 0) { /* this data is smaller than min */ }
    else if (bin >= numBins) { /* this data point is bigger than max */ }
    else {
      result[bin] += 1;
    }
  }
  return result;
}

8
投票

eDit

:这是一个例子。

double[] data = { 2, 4, 6, 7, 8, 9 }; int[] histogram = calcHistogram(data, 0, 10, 4); // This is a histogram with 4 bins, 0-2.5, 2.5-5, 5-7.5, 7.5-10. assert histogram[0] == 1; // one point (2) in range 0-2.5 assert histogram[1] == 1; // one point (4) in range 2.5-5. // etc..


there是同一功能的基于Java流的实现。

提供一些有用的范围,过滤和计数功能。 public static long[] calcHistogram(double[] data, double min, double max, int numBins) { final double interval = (max - min) / numBins; return LongStream.range(0, numBins) .map(n -> { double binStart = min + n * interval; double binEnd = min + (n + 1) * interval; return Arrays.stream(data) .filter(d -> d >= binStart && d < binEnd) .count(); }) .toArray(); }

我认为您的代码中有一个错误 - 请参阅下面的校正代码:

2
投票
public static int[] calcHistogram(double[] data, double min, double max, int numBins) { final int[] result = new int[numBins]; final double binSize = (max - min)/numBins; for (double d : data) { int bin = (int) ((d - min) / binSize); // changed this from numBins if (bin < 0) { /* this data is smaller than min */ } else if (bin >= numBins) { /* this data point is bigger than max */ } else { result[bin] += 1; } } return result; }

这是 @Altair7852的答案。
如果您想生成x值

1
投票
为y值生成x值(每个bin中的频率

histogram[] at index i)

they是x值生成方法
    private fun displayHistogram(binCount: Int, data: DoubleArray) {
        val histogram = DoubleArray(binCount)
        val distribution = org.apache.commons.math3.random.EmpiricalDistribution(binCount)
        distribution.load(data)

        var k = 0
        for (stats in distribution.binStats) {
            histogram[k++] = stats.n.toDouble()
        }

        val binSize = (data.max()!!.toDouble() - data.min()!!.toDouble()) / binCount

        for (i in 0 until histogram.size) {
            series2?.appendData(DataPoint(generateHistogramXValues(data.min()!!.toDouble(), histogram.size, binSize)[i], histogram[i]), false, histogram.count())
        }
    }


0
投票
val xValuesArray = DoubleArray(numberOfBIns) for (i in 0 until numberOfBIns) { if (i == 0){ xValuesArray[i] = min }else{ val previous = xValuesArray[i-1] xValuesArray[i] = previous+binSize } } return xValuesArray }

图形库在Android上执行此操作,但是您可以在任何lib上使用它。


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.