Normalize a matrix by an L1, L2, or maximum norm. Compute the selected norm over columns when axis=0, rows when axis=1, or the complete matrix when axis=None.
∥x∥1=i∑∣xi∣ ∥x∥2=i∑xi2 ∥x∥∞=imax∣xi∣Divide every value by the norm of its selected slice. A zero-norm slice remains zero. Return a NumPy array with the same shape as the matrix.
Input: matrix = [[3, 4], [1, 0]], axis = 1, norm_type = "l2"
Output: [[0.6, 0.8], [1.0, 0.0]]
Explanation: The row norms are 5 and 1, so each row is divided by its own norm.
Input: matrix = [[1, 2], [3, 4]], axis = 0, norm_type = "l1"
Output: [[0.25, 0.333333], [0.75, 0.666667]]
Input: matrix = [[2, 8, 4], [1, 3, 9]], axis = 1, norm_type = "max"
Output: [[0.25, 1.0, 0.5], [0.111111, 0.333333, 1.0]]
Use keepdims=True when reducing so the norm broadcasts back over the matrix.
Replace zero norms with 1.0 through np.where before dividing.
Sign in to take notes on this problem
Accepts: array
Accepts: any
Accepts: string
Normalize a matrix by an L1, L2, or maximum norm. Compute the selected norm over columns when axis=0, rows when axis=1, or the complete matrix when axis=None.
∥x∥1=i∑∣xi∣ ∥x∥2=i∑xi2 ∥x∥∞=imax∣xi∣Divide every value by the norm of its selected slice. A zero-norm slice remains zero. Return a NumPy array with the same shape as the matrix.
Input: matrix = [[3, 4], [1, 0]], axis = 1, norm_type = "l2"
Output: [[0.6, 0.8], [1.0, 0.0]]
Explanation: The row norms are 5 and 1, so each row is divided by its own norm.
Input: matrix = [[1, 2], [3, 4]], axis = 0, norm_type = "l1"
Output: [[0.25, 0.333333], [0.75, 0.666667]]
Input: matrix = [[2, 8, 4], [1, 3, 9]], axis = 1, norm_type = "max"
Output: [[0.25, 1.0, 0.5], [0.111111, 0.333333, 1.0]]
Use keepdims=True when reducing so the norm broadcasts back over the matrix.
Replace zero norms with 1.0 through np.where before dividing.
Sign in to take notes on this problem
Accepts: array
Accepts: any
Accepts: string