harmonic mean in python(python中的调和平均值)
问题描述
Python 中的谐波平均函数 (scipy.stats.hmean
) 要求输入为正数.
The Harmonic Mean function in Python (scipy.stats.hmean
) requires that the input be positive numbers.
例如:
from scipy import stats
print stats.hmean([ -50.2 , 100.5 ])
结果:
ValueError: Harmonic mean only defined if all elements greater than zero
我不明白为什么会出现这种情况,除了在极少数情况下您最终会被零除.hmean()
不是检查除以零,而是在输入任何正数时抛出错误,无论是否可以找到调和平均值.
I don't mathematically see why this should be the case, except for the rare instance where you would end up dividing by zero. Instead of checking for a divide by zero, hmean()
then throws an error upon inputing any positive number, whether a harmonic mean can be found or not.
我在数学中遗漏了什么吗?或者这真的是 SciPy
的限制吗?
Am I missing something here in the maths? Or is this really a limitation in SciPy
?
你会如何在 python 中找到一组可能是正数或负数的数字的调和平均值?
How would you go about finding the harmonic mean of a set of numbers which might be positive or negative in python?
推荐答案
调和均值仅针对正实数集定义.如果您尝试为具有负数的集合计算它,即使您的 div 没有达到 0,您也会得到各种奇怪和无用的结果. 例如,将公式应用于集合 (3, -3, 4) 给出了一个平均值共 12 个!
The harmonic mean is only defined for sets of positive real numbers. If you try and compute it for sets with negatives you get all kinds of strange and useless results even if you don't hit div by 0. For example, applying the formula to the set (3, -3, 4) gives a mean of 12!
这篇关于python中的调和平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python中的调和平均值


基础教程推荐
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01