如何在java中使用apache math 3.0生成直方图的bin?
发布时间:2020-05-25 17:16:43 所属栏目: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
解决方法据我所知,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.. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
