Implement Min-Max Scaling to normalize feature values to a specified range (typically [0, 1]). Given a dataset, transform each feature so that the minimum value maps to 0 and the maximum maps to 1.
import numpy as np
def min_max_scaling(X: np.ndarray, feature_range: tuple = (0, 1)) -> np.ndarray:
min_val = np.min(X, axis=0)
max_val = np.max(X, axis=0)
scale_min, scale_max = feature_range
denom = max_val - min_val
# Avoid division by zero for constant features
denom = np.where(denom == 0, 1, denom)
X_scaled = (X - min_val) / denom
X_scaled = X_scaled * (scale_max - scale_min) + scale_min
return X_scaled(max - min).(scale_max - scale_min) and add scale_min to map to the desired range.