Skip to content

Instantly share code, notes, and snippets.

@Glench
Created January 24, 2013 18:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glench/4626483 to your computer and use it in GitHub Desktop.
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.
import operator
def geometric_mean(iterable):
return (reduce(operator.mul, iterable)) ** (1.0/len(iterable))
@codervince
Copy link

seems reasonable to me. What issues were you expecting?
On python3, I did import functools, functools.reduce(...

@jayalane
Copy link

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