normalize rows in matrix numpy 1

normalize rows in matrix numpy

def normalize_rows(x: numpy.ndarray):
    """
    function that normalizes each row of the matrix x to have unit length.

    Args:
     ``x``: A numpy matrix of shape (n, m)

    Returns:
     ``x``: The normalized (by row) numpy matrix.
    """
    return x/numpy.linalg.norm(x, ord=2, axis=1, keepdims=True)

Here is what the above code is Doing:
1. We first compute the norm of each row of the matrix x.
2. We then divide each row of the matrix x by its corresponding norm.

Similar Posts