加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 02:53:07 所属栏目:Java 来源:网络整理
导读:我一直在寻找使用apache common math 3.0为特定数据集生成垃圾箱(通过指定低频段,高频段和所需的频段数).我看过Frequency http://commons.apache.org/math/apidocs/org/apache/commons/math3/stat/Frequency.html 但它没有给我我想要的东西..我想要一个方法,
我一直在寻找使用apache common math 3.0为特定数据集生成垃圾箱(通过指定低频段,高频段和所需的频段数).我看过Frequency http://commons.apache.org/math/apidocs/org/apache/commons/math3/stat/Frequency.html
但它没有给我我想要的东西..我想要一个方法,给我一个间隔值的频率(例如:有多少值介于0到5之间).有什么建议或想法吗?

解决方法

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

也许是这样的:

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

编辑:这是一个例子.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读