Neural Mastery
← Practice
NumPy Vectorized Min-Max Feature Scalingeasy

NumPy Vectorized Min-Max Feature Scaling

easy NumPy & Data Wrangling 10–15 min

NumPy allowed · plain Python earns 10 bonus points

Linear algebra step 1/4

A vector transforms

Matrix multiplication is the first computation behind every model.
Open lesson →
01vector
02matrix
03transform
04result
[2, −1] × [[1, 2], [−.5, 1]] = [0, −2]

Computed matrix × vector transformation

Implement vectorized min-max normalization in NumPy to scale numerical feature values into the range [0.0, 1.0].

Task

Implement min_max_scale(data). Given a 1D list of floats, scale elements to [0.0, 1.0] using (x - min) / (max - min), rounded to 4 decimal places. Return [0.0] * len(data) when max == min.

Function Signature

min_max_scale(data: list[float]) -> list[float]

Examples

Example 1: Basic Range 10-50
Input: data = [10, 20, 30, 40, 50]
Output: [0, 0.25, 0.5, 0.75, 1]
Example 2: Negative to Positive
Input: data = [-10, 0, 10]
Output: [0, 0.5, 1]
Example 3: Identical Values
Input: data = [5, 5, 5]
Output: [0, 0, 0]
Example 4: Single Element
Input: data = [42]
Output: [0]

Constraints

  • data is a list of numerical floats or ints.
  • Must handle empty list returning [].
  • Must handle identical values or single elements by returning zeros.
  • Each float in the returned list must be rounded to 4 decimal places.
Python3Saved ✓
import numpy as np

def min_max_scale(data: list[float]) -> list[float]:
"""Scale feature values into the range [0.0, 1.0] using vectorized min-max normalization.
Round each returned value to 4 decimal places.
If max == min or len(data) <= 1, return [0.0] * len(data).
If data is empty, return []."""
# Your implementation here
pass

Case 1: Basic Range 10-50
Input: data = [10, 20, 30, 40, 50]
Expected: [0, 0.25, 0.5, 0.75, 1]
Case 2: Negative to Positive
Input: data = [-10, 0, 10]
Expected: [0, 0.5, 1]
Case 3: Identical Values
Input: data = [5, 5, 5]
Expected: [0, 0, 0]
Case 4: Single Element
Input: data = [42]
Expected: [0]