Created
April 8, 2016 01:23
-
-
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
This file contains 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
## 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