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中的调和平均值
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
