How to create bins for a bar chart using apache math 3.0 in java?

I was looking to create bunkers for a specific data set (by specifying the lower band, upper band and the number of bins needed) using general apache 3.0 math. I looked at the frequency of http://commons.apache.org/math/apidocs/org/apache/commons/math3/stat/Frequency.html but it does not give me what I want .. I want a method that gives me frequency of values โ€‹โ€‹in the interval (for example: how many values โ€‹โ€‹are between 0 and 5). Any suggestions or ideas?

+8
source share
4 answers

As far as I know, Apache Commons does not have a good histogram class. I finished writing my own. If all you need is linearly distributed silos from min to max, then it's pretty easy to write.

Maybe something like this:

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

Change Here is an example.

 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.. 
+7
source

Here's an easy way to implement a histogram using 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(); } 
+15
source

I think your code has an error: see the corrected code below:

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

This is in addition to @ Altair7852's answer.

If you want to generate bin interval of x values โ€‹โ€‹for your y values โ€‹โ€‹(frequency in each histogram[] at index i) bin..aka histogram[] at index i) this is the full method

  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()) } } 

Here is a method of generating x values

  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 } 

I do this on Android with GraphView graphics library GraphView but you can use it on any library.

0
source

Source: https://habr.com/ru/post/916851/


All Articles