Skip to content

Instantly share code, notes, and snippets.

@Abusagit
Forked from f0nzie/normalize_rows.py
Created September 16, 2022 21:55
Show Gist options
  • Save Abusagit/a9a1e4eff3f7fd5bd781524595adea5e to your computer and use it in GitHub Desktop.
Save Abusagit/a9a1e4eff3f7fd5bd781524595adea5e 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