python - python中的调和均值

标签 python math statistics scipy mean

Python 中的谐波均值函数 (scipy.stats.hmean) 要求输入为正数。

例如:

from scipy import stats
print stats.hmean([ -50.2 , 100.5 ])

结果:

ValueError: Harmonic mean only defined if all elements greater than zero

我不明白为什么会出现这种情况,除了极少数情况,您最终会被零除。 hmean() 不会检查除以零,而是会在输入任何正数时抛出错误,无论是否可以找到调和平均值。

我在数学中漏掉了什么吗?或者这真的是 SciPy 的限制吗?

在 Python 中,您将如何找到一组可能为正数或负数的数字的调和平均值?

最佳答案

调和平均数仅针对正实数集定义。如果您尝试为包含负数的集合计算它,即使您没有将 div 设为 0,也会得到各种奇怪且无用的结果。例如,将公式应用于集合 (3, -3, 4) 会得到一个平均值共 12 个!

关于python - python中的调和均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10712842/

相关文章:

Python 使用 os.path.join 加入当前目录和父目录

android - 计算正在运行的车辆的 x 加速度(东)和 y 加速度(北)

python-3.x - 如何使用 rpy2 在 Jupyter 笔记本中应用 Henze-Zirkler 的多元正态性检验

python - 使用 Matplotlib 在 3d 中绘制线性模型

r - 让统计学家满意 : Stata vs. R 学生 t 检验

python - 使用 Python 搜索特定的 HTML 字符串

python - 模块未找到错误: No module named 'tensorflow.contrib.framework'

Python requests.get() : How to Print a Message if There is an Internet Connection Error?

c# - 确定数字是否在二进制序列中 1 2 4 8 16 32 64 等

python - 提示用户退出或继续