Created
September 7, 2017 00:30
-
-
Save f0nzie/7fdd9a299953ae4b2cbd666a79d67056 to your computer and use it in GitHub Desktop.
Normalize rows of a matrix by dividing rows by the normal of the matrix
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
def normalizeRows(x): | |
""" | |
Implement a function that normalizes each row of the matrix x (to have unit length). | |
Argument: | |
x -- A numpy matrix of shape (n, m) | |
Returns: | |
x -- The normalized (by row) numpy matrix. You are allowed to modify x. | |
""" | |
# Compute x_norm as the norm 2 of x. Use np.linalg.norm(..., ord = 2, axis = ..., keepdims = True) | |
x_norm = np.linalg.norm(x, axis=1, keepdims=True) | |
print("x_norm.shape:", x_norm.shape, "\n") | |
# Divide x by its norm. | |
x = x / x_norm | |
return x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment