Python中的谐波均值 [英] harmonic mean in 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中的谐波均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!