Data & Features · Easy
Feature Normalization
Implement Feature Normalization with production-minded edge case handling.
Task
Implement feature_normalization(x: list[list[float]]) -> tuple[list[list[float]], list[float], list[float]]. Standardize each feature column with its population mean and standard deviation.
Requirements
- x is a non-empty rectangular matrix whose rows are examples and columns are features.
- Compute one mean and one population standard deviation for each feature column.
- Use the population variance: divide the sum of squared deviations by the number of rows.
- If a feature has zero variance, use a scale of 1.0 so its normalized values are all zero.
- Return the normalized matrix, the feature means, and the feature scales, in that order.
- Do not mutate the input matrix.
Example
feature_normalization([[1.0, 10.0], [3.0, 20.0], [5.0, 30.0]])
# ([[-1.2247, -1.2247], [0.0, 0.0], [1.2247, 1.2247]],
# [3.0, 20.0],
# [1.6330, 8.1650])