简单统计 - 用于计算均值、标准差等的 Java 包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1735870/
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
Simple statistics - Java packages for calculating mean, standard deviation, etc
提问by Peter Perhá?
Could you please suggest any simple Java statistics packages?
你能推荐任何简单的Java统计包吗?
I don't necessarily need any of the advanced stuff. I was quite surprised that there does not appear to be a function to calculate the Mean in the java.lang.Math
package...
我不一定需要任何高级的东西。我很惊讶似乎没有一个函数来计算java.lang.Math
包中的平均值......
What are you guys using for this?
你们这是用什么?
EDIT
编辑
Regarding:
关于:
How hard is it to write a simple class that calculates means and standard deviations?
编写一个计算均值和标准差的简单类有多难?
Well, not hard. I only asked this question after having hand-coded these. But it only added to my Java frustration not to have these simplest functions available at hand when I needed them. I don't remember the formula for calculating stdev by heart :)
嗯,不难。我只是在手动编码这些之后才问这个问题。但是,当我需要这些最简单的功能时,手头没有这些最简单的功能,这只会增加我的 Java 挫败感。我不记得用心计算 stdev 的公式了 :)
采纳答案by John Paulett
回答by Stephen C
Just responding to this part of the question:
仅回答问题的这一部分:
I was quite surprised that there does not appear to be a function to calculate the Mean in the java.lang.Math package...
我很惊讶在 java.lang.Math 包中似乎没有计算平均值的函数......
I don't think I was surprised to find this. There are a lot of "useful algorithms" that the Java class libraries do not implement. They do not implement everything. And in this, they are no different from other programming languages.
我不认为我对发现这一点感到惊讶。Java 类库没有实现很多“有用的算法”。他们并没有实施一切。而在这一点上,它们与其他编程语言没有什么不同。
Actually It would be a bad thing if Sun did try to implement too much in J2SE:
实际上,如果 Sun 确实尝试在 J2SE 中实现太多,那将是一件坏事:
It would take more designer / developer / technical documenter time ... with no clear "return on investment".
It would increase the Java footprint; e.g. the size of "rt.jar". (Or if they tried to mitigate that, it would result in more platform complexity ... )
For things in the mathematical space, you often need to implement the algorithms in different ways (with different APIs) to cater for different requirements.
For complex things, it may be better for Sun not to try to "standardise" the APIs, but leave it to some other interested / skilled group to do it; e.g. the Apache folks.
这将需要更多的设计师/开发人员/技术文档编制人员的时间......没有明确的“投资回报”。
它会增加 Java 占用空间;例如“rt.jar”的大小。(或者,如果他们试图减轻这种情况,则会导致更多的平台复杂性......)
对于数学空间中的事物,您通常需要以不同的方式(使用不同的 API)来实现算法以满足不同的需求。
对于复杂的事情,Sun 最好不要尝试“标准化”API,而是让其他一些感兴趣/熟练的团队来做;例如阿帕奇人。
回答by user889392
I think there is no direct method and classes in java. We have to build it for our own. For your requirement this code will help you. Calculate Standard Deviation in java
我认为java中没有直接的方法和类。我们必须为我们自己建造它。根据您的要求,此代码将帮助您。在java中计算标准偏差
回答by hodan_egal
import java.util.*;
public class stdevClass {
public static void main(String[] args){
int [] list = {1,-2,4,-4,9,-6,16,-8,25,-10};
double stdev_Result = stdev(list);
System.out.println(stdev(list));
}
public static double stdev(int[] list){
double sum = 0.0;
double mean = 0.0;
double num=0.0;
double numi = 0.0;
double deno = 0.0;
for (int i : list) {
sum+=i;
}
mean = sum/list.length;
for (int i : list) {
numi = Math.pow((double) i - mean), 2);
num+=numi;
}
return Math.sqrt(num/list.length);
}
}
回答by Peter Perhá?
Since Java SE 8 a number of classes has been added to the platform:
自 Java SE 8 以来,该平台添加了许多类: