Skip to content

Commit 9f83ff7

Browse files
REFACTOR: Apply saved hyperparameters to models before evaluation in main function in stacking.py
1 parent 91e0d12 commit 9f83ff7

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

stacking.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,12 @@ def main():
916916
X_train_scaled, X_test_scaled, y_train, y_test, scaler = scale_and_split(X_full, y) # Scale and split the data
917917

918918
base_models = get_models() # Get the base models
919+
920+
hp_results_raw = extract_hyperparameter_optimization_results(file) # Extract hyperparameter optimization results
921+
if hp_results_raw: # If results were found, extract the params mapping and apply
922+
hp_params_map = {k: (v.get("best_params") if isinstance(v, dict) else v) for k, v in hp_results_raw.items()} # Extract only the best_params mapping
923+
base_models = apply_hyperparameters_to_models(hp_params_map, base_models) # Apply hyperparameters to base models
924+
919925
estimators = [(name, model) for name, model in base_models.items() if name != "SVM"] # Define estimators (excluding SVM)
920926

921927
stacking_model = StackingClassifier(estimators=estimators, final_estimator=RandomForestClassifier(n_estimators=50, random_state=42), cv=5, n_jobs=1) # Define the Stacking Classifier model
@@ -932,7 +938,7 @@ def main():
932938
feature_sets = {k: v for k, v in feature_sets.items() if v is not None} # Remove any None entries (e.g., PCA if not applied)
933939
feature_sets = dict(sorted(feature_sets.items())) # Sort the feature sets by name
934940

935-
individual_models = get_models() # Get all individual models
941+
individual_models = {k: v for k, v in base_models.items()} # Use the base models (with hyperparameters applied) for individual evaluation
936942
total_steps = len(feature_sets) * (len(individual_models) + 1) # Total steps: models + stacking per feature set
937943
progress_bar = tqdm(total=total_steps) # Progress bar for all evaluations
938944

0 commit comments

Comments
 (0)