java 如何在java中使用apache math 3.0为直方图生成bins?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10786465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to generate bins for histogram using apache math 3.0 in java?
提问by Sami
I have been looking for away to generate bins for specific dataset (by specifying lower band, upper band and number of bins required) using apache common math 3.0. I have looked at Frequency http://commons.apache.org/math/apidocs/org/apache/commons/math3/stat/Frequency.htmlbut it does not give me what i want.. i want a method that give me frequency for values in an interval ( ex: how many values are between 0 to 5). Any suggestions or ideas?
我一直在寻找使用 apache common math 3.0 为特定数据集生成 bins(通过指定下带、上带和所需的 bin 数量)的方法。我看过频率http://commons.apache.org/math/apidocs/org/apache/commons/math3/stat/Frequency.html但它没有给我我想要的..我想要一种方法给我间隔中值的频率(例如:0 到 5 之间的值有多少)。有什么建议或想法吗?
采纳答案by Max
As far as I know there is no good histogram class in Apache Commons. I ended up writing my own. If all you want are linearly distributed bins from min to max, then it is quite easy to write.
据我所知,Apache Commons 中没有好的直方图类。我最终写了自己的。如果你想要的只是从最小值到最大值的线性分布的 bin,那么写起来就很容易了。
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;
}
Edit: Here's 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..
回答by Altair7852
Here is a simple way to implement histogram using Apache Commons Math 3:
这是使用 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();
}
回答by user1172468
I think your code has a bug in it -- please 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;
}
回答by Aleckson Nyamwaya
This is in addition to @Altair7852's answer.
这是对@Altair7852 的回答的补充。
If you want to generate x values bin interval
for your y values (the frequency in each bin..akahistogram[] at index i)
here is the full method
如果你想bin interval
为你的 y 值生成 x 值(每个 bin 中的频率..akahistogram[] at index i)
这里是完整的方法
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 the x values generating method
这是x值生成方法
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'm doing this on android using GraphView
graphing library but you can use this on any lib.
我正在使用GraphView
图形库在 android 上执行此操作,但您可以在任何库上使用它。