SIENTIAPDE-1430: Implement advanced model training capabilities and enhanced data preprocessing. This includes support for Polynomial Regression with configurable degree and interaction terms, flexible per-variable lag configurations, and new data filtering options by date range and removed intervals. Comprehensive business validations are now enforced for all parameters, and MLflow logging has been extended to capture these detailed configurations. Additionally, Reduced Coulomb Energy (RCE) metrics are added for drift detection, with a new changelog documenting all pipeline parameter updates.

This commit is contained in:
Bruno Domingues
2025-12-17 21:39:43 -03:00
parent 4d6674758c
commit 6e8f87b2a3
10 changed files with 796 additions and 81 deletions

View File

@@ -88,6 +88,8 @@ class TrainingRepository:
regr = LinearRegressionModel(
target_variable=params.target_variable,
variable_columns=params.variable_columns,
degree=params.degree,
interaction_only=params.interaction_only,
)
regr.fit(data_train)
@@ -236,20 +238,27 @@ class TrainingRepository:
Returns:
DataPreprocessor: Configured preprocessor ready for fitting
"""
# Create lag dictionaries for each variable
lag_train_dict = dict.fromkeys(params.variable_columns, params.lag_train)
lag_val_dict = dict.fromkeys(params.variable_columns, params.lag_val)
# Convert removed_intervals to list of tuples if needed
removed_intervals = None
if params.removed_intervals:
removed_intervals = [
(interval[0], interval[1]) if isinstance(interval, (list, tuple)) else interval
for interval in params.removed_intervals
]
return DataPreprocessor(
target_variable=params.target_variable,
input_columns=params.variable_columns,
lag_train=lag_train_dict,
lag_transform=lag_val_dict,
nan_treatment=params.nan_treatment,
lag_train=params.lag_train,
lag_transform=params.lag_val,
start_date=params.start_date,
end_date=params.end_date,
removed_intervals=removed_intervals,
static_threshold=1 if params.rem_static_win else None,
low_lim=params.low_lim,
upp_lim=params.upp_lim,
window=params.window,
scaler_name='Standard Scaler' if params.use_scaler else 'None',
scaler_name=params.scaler_name,
scaler_params={} if params.use_scaler else None,
ar_var=params.target_variable if params.include_ar else None,
)
@@ -279,10 +288,17 @@ class TrainingRepository:
coefficients = regr.regr.coef_
intercept = regr.regr.intercept_
# Get feature names - for polynomial models, use poly_feature_names
if params.degree > 1 and regr.poly_feature_names:
feature_names = regr.poly_feature_names
else:
feature_names = params.variable_columns
# Create coefficients dictionary
coefficients_dict = {}
for i, var in enumerate(params.variable_columns):
coefficients_dict[var] = float(coefficients[i])
for i, var in enumerate(feature_names):
if i < len(coefficients):
coefficients_dict[var] = float(coefficients[i])
# Create equation string
equation_parts = [f'{coef:.6f} * {var}' for var, coef in coefficients_dict.items()]
@@ -300,5 +316,8 @@ class TrainingRepository:
'intercept': float(intercept),
'equation_string': equation_string,
'latex_equation': latex_equation,
'model_type': 'Linear Regression',
'model_type': params.model_name,
'degree': params.degree,
'interaction_only': params.interaction_only,
'original_features': params.variable_columns,
}