← Back to the chapter · Next detail →
Technical detail · Feature Scaling Lab: Statistical Analysis
Illustrative normalizer selector
The selector classes are illustrative: ZScoreNormalizer and LogTransform are undefined placeholders; RobustScaler and MinMaxScaler require imports from sklearn.preprocessing.
def select_normalizer(data_characteristics):
"""
Select appropriate normalization based on data characteristics.
Returns: (normalizer_class, parameters)
"""
has_outliers = data_characteristics['outlier_rate'] > 0.05
is_neural_network = data_characteristics['model_type'] == 'neural_network'
has_mixed_scales = data_characteristics['mixed_feature_scales']
is_skewed = data_characteristics['skewness'] > 2.0
if has_outliers:
return (RobustScaler, {})
elif is_neural_network:
return (MinMaxScaler, {'feature_range': (0, 1)})
elif has_mixed_scales:
return (ZScoreNormalizer, {})
elif is_skewed:
return (LogTransform, {'then_zscore': True})
else:
return (MinMaxScaler, {'feature_range': (0, 1)})
# Usage example
characteristics = {
'outlier_rate': 0.08, # 8% outliers
'model_type': 'kmeans',
'mixed_feature_scales': True,
'distribution': 'unknown',
'skewness': 1.2
}
normalizer_class, params = select_normalizer(characteristics)
# Returns: RobustScaler (because outlier_rate > 0.05)