Problems
Loading...
1 / 1

Implement Matrix Normalization

Linear AlgebraData Processing
Medium

Implement matrix normalization that can normalize along different axes using different norm types.

Normalization Types

L2 Norm (Euclidean)

x2=ixi2||x||_2 = \sqrt{\sum_{i} x_i^2}

L1 Norm (Manhattan)

x1=ixi||x||_1 = \sum_{i} |x_i|

Max Norm (Infinity)

x=maxixi||x||_{\infty} = \max_{i} |x_i|

Parameters

  • matrix: 2D array-like input
  • axis: 0 (column-wise), 1 (row-wise), or None (entire matrix)
  • norm_type: 'l1', 'l2', or 'max'
Loading visualization...

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)
Try Similar Problems