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? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01