Implement Matrix Normalization
Implement Matrix Normalization
Implement matrix normalization that can normalize along different axes using different norm types.
Normalization Types
L2 Norm (Euclidean)
∣∣x∣∣2=i∑xi2L1 Norm (Manhattan)
∣∣x∣∣1=i∑∣xi∣Max Norm (Infinity)
∣∣x∣∣∞=imax∣xi∣Parameters
matrix: 2D array-like inputaxis: 0 (column-wise), 1 (row-wise), or None (entire matrix)norm_type: 'l1', 'l2', or 'max'
Examples
Input: [[3,4],[1,0]], axis=1, norm_type='l2'
Output: [[0.6, 0.8], [1.0, 0.0]]
Row-wise L2: [3,4] → [3/5, 4/5], [1,0] → [1,0]
Input: [[1,2],[3,4]], axis=0, norm_type='l1'
Output: [[0.25, 1/3], [0.75, 2/3]]
Column-wise L1: col1: [1,3] → [1/4, 3/4], col2: [2,4] → [2/6, 4/6]
Input: [[2,8],[4,2]], axis=1, norm_type='max'
Output: [[0.25, 1.0], [1.0, 0.5]]
Row-wise Max: [2,8] → [2/8, 8/8], [4,2] → [4/4, 2/4]
Hint 1
Use NumPy's built-in functions like np.sum(), np.sqrt(), and np.max() with the axis parameter. Remember to use keepdims=True for proper broadcasting during division.
Requirements
- Must work with lists or NumPy arrays
- Must return a NumPy array for valid inputs
- Must return None for invalid inputs (simple error handling)
- Must handle zero vectors (avoid division by zero)
Constraints
- Time limit: 500 ms, Memory: 128 MB
- NumPy only (no sklearn, scipy)
Log in to take notes on this problem
Accepts: array
Accepts: any
Accepts: string
Implement Matrix Normalization
Implement Matrix Normalization
Implement matrix normalization that can normalize along different axes using different norm types.
Normalization Types
L2 Norm (Euclidean)
∣∣x∣∣2=i∑xi2L1 Norm (Manhattan)
∣∣x∣∣1=i∑∣xi∣Max Norm (Infinity)
∣∣x∣∣∞=imax∣xi∣Parameters
matrix: 2D array-like inputaxis: 0 (column-wise), 1 (row-wise), or None (entire matrix)norm_type: 'l1', 'l2', or 'max'
Examples
Input: [[3,4],[1,0]], axis=1, norm_type='l2'
Output: [[0.6, 0.8], [1.0, 0.0]]
Row-wise L2: [3,4] → [3/5, 4/5], [1,0] → [1,0]
Input: [[1,2],[3,4]], axis=0, norm_type='l1'
Output: [[0.25, 1/3], [0.75, 2/3]]
Column-wise L1: col1: [1,3] → [1/4, 3/4], col2: [2,4] → [2/6, 4/6]
Input: [[2,8],[4,2]], axis=1, norm_type='max'
Output: [[0.25, 1.0], [1.0, 0.5]]
Row-wise Max: [2,8] → [2/8, 8/8], [4,2] → [4/4, 2/4]
Hint 1
Use NumPy's built-in functions like np.sum(), np.sqrt(), and np.max() with the axis parameter. Remember to use keepdims=True for proper broadcasting during division.
Requirements
- Must work with lists or NumPy arrays
- Must return a NumPy array for valid inputs
- Must return None for invalid inputs (simple error handling)
- Must handle zero vectors (avoid division by zero)
Constraints
- Time limit: 500 ms, Memory: 128 MB
- NumPy only (no sklearn, scipy)
Log in to take notes on this problem
Accepts: array
Accepts: any
Accepts: string