Created
January 24, 2013 18:59
-
-
Save Glench/4626483 to your computer and use it in GitHub Desktop.
Geometric mean function for python. Takes the nth root of all values in an iterable multiplied together. There might be some issues with this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
def geometric_mean(iterable): | |
return (reduce(operator.mul, iterable)) ** (1.0/len(iterable)) |
I think that there is a risk of overflow here. The safer way is to log each number, take the arithmetic mean, then anti-log.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
seems reasonable to me. What issues were you expecting?
On python3, I did import functools, functools.reduce(...