Per-Column Min-Max Scaling in NumPy

Quick Overview

Implement per-column min-max scaling of a 2D NumPy array so every column is rescaled to the range 0 to 1. It tests axis reductions with keepdims, broadcasting without Python loops, and how to deal with constant columns and train/test scaling.

Per-Column Min-Max Scaling in NumPy

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `minmax_columns(X)` that rescales each column of a 2D NumPy array into `[0, 1]` via `(col - col_min) / (col_max - col_min)`. Return a same-shape float array. Requirements: - Compute `col_min` and `col_max` with `X.min(axis=0, keepdims=True)` and `X.max(axis=0, keepdims=True)`. - Rescale via broadcasting, with no Python loops over columns. For example: ```python X = np.array([[1., 2.], [3., 6.], [5., 4.]]) minmax_columns(X) # array([[0. , 0. ], # [0.5, 1. ], # [1. , 0.5]]) ``` ```hint Shapes Write down the shape of the column statistics with and without `keepdims=True`, and check which one lines up against an `(n_rows, n_cols)` array under NumPy's broadcasting rules. ``` ### What a Strong Answer Covers - Which axis the reduction runs along and why that gives one statistic per column - The shapes of `col_min`, `col_max` and `X`, and how broadcasting aligns them - A fully vectorized expression with no loop over columns - The output dtype and whether the input array is modified ### Follow-up Questions - How would you handle a constant column where `col_max == col_min`? - How would you generalize this to scale rows instead of columns? - If you fit the scaling on a training set, how do you apply it to a test set, and what can happen to the test values?

Overview: Implement per-column min-max scaling of a 2D NumPy array so every column is rescaled to the range 0 to 1. It tests axis reductions with keepdims, broadcasting without Python loops, and how to deal with constant columns and train/test scaling.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement minmax_columns(X) that rescales each column of a 2D NumPy array into [0, 1] via (col - col_min) / (col_max - col_min). Return a same-shape float array.

Requirements:

  • Compute col_min and col_max with X.min(axis=0, keepdims=True) and X.max(axis=0, keepdims=True) .
  • Rescale via broadcasting, with no Python loops over columns.

For example:

X = np.array([[1., 2.],
              [3., 6.],
              [5., 4.]])
minmax_columns(X)
# array([[0. , 0. ],
#        [0.5, 1. ],
#        [1. , 0.5]])

What a Strong Answer Covers Guidance

  • Which axis the reduction runs along and why that gives one statistic per column
  • The shapes of col_min , col_max and X , and how broadcasting aligns them
  • A fully vectorized expression with no loop over columns
  • The output dtype and whether the input array is modified

Follow-up Questions Guidance

  • How would you handle a constant column where col_max == col_min ?
  • How would you generalize this to scale rows instead of columns?
  • If you fit the scaling on a training set, how do you apply it to a test set, and what can happen to the test values?