How to use Apache math 3.0 to generate bin of histogram in Java?
•
Java
I've been looking for using Apache common math 3.0 to generate dustbins for specific data sets (by specifying low frequency band, high frequency band and the number of frequency bands required) I've seen frequency http://commons.apache.org/math/apidocs/org/apache/commons/math3/stat/Frequency.html
Solution
As far as I know, there is no good histogram class in Apache Commons I finally wrote my own If you want a linear distribution box from minimum to maximum, it's easy to write
Maybe it's 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; }
Editor: This is an example
double[] data = { 2,4,6,7,8,9 }; int[] histogram = calcHistogram(data,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..
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码