Min-Max Scaling
Min-Max Scaling
Min-max scaling (normalization) transforms each feature column to the [0, 1] range. The minimum value becomes 0, the maximum becomes 1, and all other values are linearly scaled between them. This ensures all features have the same scale, which is critical for distance-based models (KNN, SVM) and neural networks.
Given a data matrix (list of rows), scale each column independently to the [0, 1] range.
Algorithm
For each column j:
xij′=maxj−minjxij−minjIf all values in a column are the same (range is 0), set the scaled values to 0.0.
Examples
Input:
data = [[1, 10], [2, 20], [3, 30]]
Output:
[[0.0, 0.0], [0.5, 0.5], [1.0, 1.0]]
Column 0: min=1, max=3, range=2. (1-1)/2=0.0, (2-1)/2=0.5, (3-1)/2=1.0. Column 1 scales similarly.
Input:
data = [[0, 0], [10, 100], [20, 50]]
Output:
[[0.0, 0.0], [0.5, 1.0], [1.0, 0.5]]
Column 0: 0->0.0, 10->0.5, 20->1.0. Column 1: 0->0.0, 100->1.0, 50->0.5. Each column is scaled independently.
Hint 1
Process column by column. For each column j, extract all values, find min and max, compute range. Then for each row, apply (value - min) / range. Handle the zero-range case separately.
Hint 2
Create the result matrix first (same dimensions, filled with 0.0). Loop over columns j from 0 to n_cols-1, extract the column, compute min/max/range, then fill in the scaled values for that column.
Requirements
- Scale each column independently to [0, 1]
- If a column has zero range (all values identical), set scaled values to 0.0
- Return a list of lists with the same shape as the input
- Return floats
Constraints
- data is a non-empty rectangular matrix
- Values are numbers (int or float)
- Return a list of lists of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Min-Max Scaling
Min-Max Scaling
Min-max scaling (normalization) transforms each feature column to the [0, 1] range. The minimum value becomes 0, the maximum becomes 1, and all other values are linearly scaled between them. This ensures all features have the same scale, which is critical for distance-based models (KNN, SVM) and neural networks.
Given a data matrix (list of rows), scale each column independently to the [0, 1] range.
Algorithm
For each column j:
xij′=maxj−minjxij−minjIf all values in a column are the same (range is 0), set the scaled values to 0.0.
Examples
Input:
data = [[1, 10], [2, 20], [3, 30]]
Output:
[[0.0, 0.0], [0.5, 0.5], [1.0, 1.0]]
Column 0: min=1, max=3, range=2. (1-1)/2=0.0, (2-1)/2=0.5, (3-1)/2=1.0. Column 1 scales similarly.
Input:
data = [[0, 0], [10, 100], [20, 50]]
Output:
[[0.0, 0.0], [0.5, 1.0], [1.0, 0.5]]
Column 0: 0->0.0, 10->0.5, 20->1.0. Column 1: 0->0.0, 100->1.0, 50->0.5. Each column is scaled independently.
Hint 1
Process column by column. For each column j, extract all values, find min and max, compute range. Then for each row, apply (value - min) / range. Handle the zero-range case separately.
Hint 2
Create the result matrix first (same dimensions, filled with 0.0). Loop over columns j from 0 to n_cols-1, extract the column, compute min/max/range, then fill in the scaled values for that column.
Requirements
- Scale each column independently to [0, 1]
- If a column has zero range (all values identical), set scaled values to 0.0
- Return a list of lists with the same shape as the input
- Return floats
Constraints
- data is a non-empty rectangular matrix
- Values are numbers (int or float)
- Return a list of lists of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array