normalize rows in matrix numpy

Solutions on MaxInterview for normalize rows in matrix numpy by the best coders in the world

showing results for - "normalize rows in matrix numpy"
Arianna
02 Oct 2019
1def normalize_rows(x: numpy.ndarray):
2    """
3    function that normalizes each row of the matrix x to have unit length.
4
5    Args:
6     ``x``: A numpy matrix of shape (n, m)
7
8    Returns:
9     ``x``: The normalized (by row) numpy matrix.
10    """
11    return x/numpy.linalg.norm(x, ord=2, axis=1, keepdims=True)
12