如何在java中使用apache math 3.0生成直方图的bin?

前端之家收集整理的这篇文章主要介绍了如何在java中使用apache math 3.0生成直方图的bin?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在寻找使用apache common math 3.0为特定数据集生成垃圾箱(通过指定低频段,高频段和所需的频段数).我看过Frequency http://commons.apache.org/math/apidocs/org/apache/commons/math3/stat/Frequency.html
但它没有给我我想要的东西..我想要一个方法,给我一个间隔值的频率(例如:有多少值介于0到5之间).有什么建议或想法吗?

解决方法

据我所知,Apache Commons中没有好的直方图类.我最终写了自己的.如果你想要的是从最小到最大的线性分布箱,那么它很容易编写.

也许是这样的:

  1. public static int[] calcHistogram(double[] data,double min,double max,int numBins) {
  2. final int[] result = new int[numBins];
  3. final double binSize = (max - min)/numBins;
  4.  
  5. for (double d : data) {
  6. int bin = (int) ((d - min) / binSize);
  7. if (bin < 0) { /* this data is smaller than min */ }
  8. else if (bin >= numBins) { /* this data point is bigger than max */ }
  9. else {
  10. result[bin] += 1;
  11. }
  12. }
  13. return result;
  14. }

编辑:这是一个例子.

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

猜你在找的Java相关文章