InterviewPrepKit

Home / Coding / Machine Learning Coding / Numerical & Data / Min-Max Scaling

Min-Max Scaling

easy 00:00
Solving tips
  • Scale each column independently: reduce along axis=0 so min and max are per-feature vectors of shape (n_features,).
  • A constant column has max == min, so the denominator is zero — decide its output up front instead of dividing.
  • Broadcasting (X - col_min) / col_range works when col_min and col_range have shape (n_features,).

Implement min-max scaling from scratch with NumPy, without using sklearn.preprocessing.MinMaxScaler. Min-max scaling is a standard preprocessing step that rescales each feature to a fixed range so that features on different scales contribute comparably to distance-based and gradient-based models.

Definition

For each column independently, with per-column minimum min and maximum max:

x_scaled = (x - min) / (max - min)

When a column is constant (max == min), the denominator is zero; map every value in that column to 0.

Task

Complete min_max_scale(X) so it returns a new array of the same shape where every column is scaled to [0, 1] using the formula above. Scale each column independently. Constant columns become all zeros. Do not call sklearn or any built-in scaler.

Example

X = np.array([[1.0, 10.0, 5.0],
              [2.0, 20.0, 5.0],
              [3.0, 30.0, 5.0]])
min_max_scale(X)
# -> array([[0. , 0. , 0. ],
#           [0.5, 0.5, 0. ],
#           [1. , 1. , 0. ]])

Columns 0 and 1 range from their min to max linearly onto [0, 1]. Column 2 is constant (5, 5, 5), so it maps to all zeros.

Constraints

  • 1 <= n_samples, 1 <= n_features; use vectorized NumPy, not Python loops over elements.
  • Values fit in float64.
  • The output must not modify the input array in place.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug