Back to Practice
#0058
Min-Max Scale an Array
EasyNumPy15 min15 XP
Problem
Given a NumPy array, return a scaled array where the minimum becomes 0 and the maximum becomes 1. If all values are equal, return an array of zeros with the same shape.
Why This Matters
Feature scaling is a small operation with large model impact. Implementing it once makes sklearn preprocessing feel less magical.
Function Signature
def minmax_scale(values):
Examples
Example 1
Inputvalues = np.array([10, 20, 30])
Outputarray([0.0, 0.5, 1.0])
10 is the minimum, 30 is the maximum, and 20 sits halfway.
Constraints
- Return floats.
- Preserve the input shape.
- Handle constant arrays safely.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Simple increasing vector
Inputnp.array([10, 20, 30])
Expectednp.array([0.0, 0.5, 1.0])
Values are placed proportionally between the min and max.
Constant values
Inputnp.array([5, 5, 5])
Expectednp.array([0.0, 0.0, 0.0])
A zero denominator would be invalid, so constant data maps to zeros.
Hidden Test Categories
2D inputnegative valuesfloat input