Skip to content

Instantly share code, notes, and snippets.

@f0nzie
Created September 7, 2017 00:30
Show Gist options
  • Save f0nzie/7fdd9a299953ae4b2cbd666a79d67056 to your computer and use it in GitHub Desktop.
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
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