Skip to content

Instantly share code, notes, and snippets.

@Mahdisadjadi
Created April 8, 2016 01:23
Show Gist options
  • Save Mahdisadjadi/381be6a58801e30c08eed22058a0e187 to your computer and use it in GitHub Desktop.
Save Mahdisadjadi/381be6a58801e30c08eed22058a0e187 to your computer and use it in GitHub Desktop.
Numpy function to normalize an array with arbirtray shape in respect to different norms and arbitrary axis
## All the credit goes to Eelco Hoogendoorn
## http://stackoverflow.com/a/21032099/3349443
def normalized(a, axis=-1, order=2):
norms = np.atleast_1d(np.linalg.norm(a, order, axis))
norms[norms==0] = 1
return a / np.expand_dims(norms, axis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment