TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Matrix Normalization

Linear AlgebraData Processing
Medium

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∣\lVert x \rVert_1 = \sum_i |x_i|∥x∥1​=i∑​∣xi​∣ ∥x∥2=∑ixi2\lVert x \rVert_2 = \sqrt{\sum_i x_i^2}∥x∥2​=i∑​xi2​​ ∥x∥∞=max⁡i∣xi∣\lVert x \rVert_{\infty} = \max_i |x_i|∥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.

Loading visualization...

Examples

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]]

Hint 1

Use keepdims=True when reducing so the norm broadcasts back over the matrix.

Hint 2

Replace zero norms with 1.0 through np.where before dividing.

Requirements

  • Support l1, l2, and max norms
  • Support axis 0, axis 1, and axis None
  • Preserve zero slices without dividing by zero
  • Return a NumPy array

Constraints

  • matrix is a nonempty rectangular two-dimensional numeric list
  • axis is 0, 1, or None
  • norm_type is l1, l2, or max
  • Use NumPy only
Try Similar Problems
Zscore StandardizationMinmax NormalizationPca ProjectionCovariance MatrixPearson Correlation

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: any

Accepts: string

You must run your code first.
PrevNext

Implement Matrix Normalization

Linear AlgebraData Processing
Medium

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∣\lVert x \rVert_1 = \sum_i |x_i|∥x∥1​=i∑​∣xi​∣ ∥x∥2=∑ixi2\lVert x \rVert_2 = \sqrt{\sum_i x_i^2}∥x∥2​=i∑​xi2​​ ∥x∥∞=max⁡i∣xi∣\lVert x \rVert_{\infty} = \max_i |x_i|∥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.

Loading visualization...

Examples

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]]

Hint 1

Use keepdims=True when reducing so the norm broadcasts back over the matrix.

Hint 2

Replace zero norms with 1.0 through np.where before dividing.

Requirements

  • Support l1, l2, and max norms
  • Support axis 0, axis 1, and axis None
  • Preserve zero slices without dividing by zero
  • Return a NumPy array

Constraints

  • matrix is a nonempty rectangular two-dimensional numeric list
  • axis is 0, 1, or None
  • norm_type is l1, l2, or max
  • Use NumPy only
Try Similar Problems
Zscore StandardizationMinmax NormalizationPca ProjectionCovariance MatrixPearson Correlation

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: any

Accepts: string

You must run your code first.