Skip to content

Instantly share code, notes, and snippets.

@syrte
Created March 23, 2016 01:54
Show Gist options
  • Save syrte/b5da1ee6e4a7661064c6 to your computer and use it in GitHub Desktop.
Save syrte/b5da1ee6e4a7661064c6 to your computer and use it in GitHub Desktop.
Return mean value of adjacent member of an array. Useful for plotting bin counts.
def mid(x, base=None):
'''Return mean value of adjacent member of an array.
Useful for plotting bin counts.
'''
if base is None:
x = np.asarray(x)
return (x[1:] + x[:-1])/2.
elif base == 'log':
return np.exp(mid(np.log(x)))
elif base == 'exp':
return np.log(mid(np.exp(x)))
else:
assert base > 0
return np.log(mid(base**x))/np.log(base)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment