如何计算一组数字的平均值、中位数、众数和极差
是否有任何函数(作为数学库的一部分)可以计算平均值、一组数字的中位数、众数和范围。
Are there any functions (as part of a math library) which will calculate mean, median, mode and range from a set of numbers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,似乎确实有第三个库(Java Math 中没有)。出现的两个是:
http://opsresearch.com/app/
http://www.iro.umontreal.ca/~simardr/ssj/indexe.html
但是,编写自己的方法来计算均值、中位数、众数和极差实际上并不困难。
MEAN
MEDIAN
MODE
UPDATE
正如 Neelesh Salpe 所指出的,上述内容并不适合多模式集合。我们可以很容易地解决这个问题:
ADDITION
如果您使用的是 Java 8 或更高版本,您还可以像这样确定模式:
Yes, there does seem to be 3rd libraries (none in Java Math). Two that have come up are:
http://opsresearch.com/app/
http://www.iro.umontreal.ca/~simardr/ssj/indexe.html
but, it is actually not that difficult to write your own methods to calculate mean, median, mode and range.
MEAN
MEDIAN
MODE
UPDATE
As has been pointed out by Neelesh Salpe, the above does not cater for multi-modal collections. We can fix this quite easily:
ADDITION
If you are using Java 8 or higher, you can also determine the modes like this:
查看 apache 的公共数学。那里有很多。
Check out commons math from apache. There is quite a lot there.
如果您只关心单峰分布,请考虑某事。像这样。
If you only care about unimodal distributions, consider sth. like this.
测试数据
Test data
正如 Nico Huysamen 已经指出的,在 Java 1.8 中查找多种模式可以通过以下方式完成。
快乐编码!
As already pointed out by Nico Huysamen, finding multiple mode in Java 1.8 can be done alternatively as below.
Happy coding!
这是 JAVA 8 中完整、干净且优化的代码
Here's the complete clean and optimised code in JAVA 8