Pipeline optimizer#
Main PipelineOptimizer class implementation.
- class spectoprep.pipeline.optimizer.PipelineOptimizer(X_train, y_train, preprocessing_steps=None, X_test=None, y_test=None, cv_method='group_shuffle_split', n_splits=3, test_size=0.3, n_groups_out=2, random_state=42, groups=None, max_pipeline_length=5, n_jobs=-1, allowed_preprocess_combinations=None, log_level='INFO')[source]#
Bases:
objectA class for optimizing machine learning pipelines using Bayesian optimization. It precomputes possible pipeline configurations and then searches over both the pipeline configuration (encoded as an index) and the hyperparameters.
- Parameters:
X_train (NDArray)
y_train (NDArray)
X_test (NDArray | None)
y_test (NDArray | None)
cv_method (str)
n_splits (int)
test_size (float)
n_groups_out (int)
random_state (int)
groups (NDArray | None)
max_pipeline_length (int)
n_jobs (int)
allowed_preprocess_combinations (int | list[int] | tuple[int, ...] | None)
log_level (str)
- __init__(X_train, y_train, preprocessing_steps=None, X_test=None, y_test=None, cv_method='group_shuffle_split', n_splits=3, test_size=0.3, n_groups_out=2, random_state=42, groups=None, max_pipeline_length=5, n_jobs=-1, allowed_preprocess_combinations=None, log_level='INFO')[source]#
Initialize the PipelineOptimizer.
- Parameters:
X_train (NDArray) – Training features.
y_train (NDArray) – Training targets.
preprocessing_steps (list[str] | str | None) – List of preprocessing steps to use.
X_test (NDArray | None) – Test features (optional).
y_test (NDArray | None) – Test targets (optional).
cv_method (str) – One of “group_shuffle_split”, “group_kfold”, or “leave_p_group_out”.
n_splits (int) – Number of CV splits.
test_size (float) – Test set fraction (if using GroupShuffleSplit).
n_groups_out (int) – Number of groups left out (if using LeavePGroupsOut).
random_state (int) – Random seed.
groups (NDArray | None) – Optional group labels; if None, one group per sample is used.
max_pipeline_length (int) – Maximum number of steps in pipeline.
n_jobs (int) – Number of parallel jobs for compatible estimators.
allowed_preprocess_combinations (int | list[int] | tuple[int, ...] | None) – Allowed lengths for preprocessing combinations.
log_level (str) – Logging level (INFO, DEBUG, WARNING, ERROR).
- bayes_objective(**params)[source]#
Objective function for Bayesian optimization.
The score is the negative mean RMSE under group-aware cross-validation on the training data only. Any supplied test set is deliberately never touched here: using it to select preprocessing or hyperparameters would leak the held-out data and inflate reported performance. The test set is used only for final reporting in
get_best_pipeline_predictions().- Parameters:
**params – Parameters proposed by the Bayesian optimizer.
- Returns:
Negative cross-validated RMSE, or a large penalty (
-1e6) if no valid predictions could be produced.- Return type:
- bayesian_optimize(init_points=10, n_iter=50, acquisition_function='ei')[source]#
Run Bayesian optimization to find the best pipeline configuration and hyperparameters.
- Parameters:
- Returns:
Dict of best parameters
Fitted Pipeline with best configuration
- Return type:
Tuple containing
- get_best_pipeline_predictions(best_pipeline)[source]#
Get predictions using the best pipeline.
- Parameters:
best_pipeline (Pipeline) – Fitted pipeline object
- Returns:
Predictions array
RMSE score
R² score
- Return type:
Tuple containing
- print_evaluated_pipelines()[source]#
Print details for all evaluated pipelines from the Bayesian optimizer.
This method assumes that bayesian_optimize() has been run and that self.optimizer exists.
- Return type:
None
- export_best_pipeline(file_path)[source]#
Export the best pipeline configuration and hyperparameters to a file.
- Parameters:
file_path (str) – Path to save the export file
- Raises:
AttributeError – If optimizer hasn’t been run yet
- Return type:
None
Configuration#
Configuration constants for the ML Pipeline Optimizer.
Builder utilities#
Functions for building preprocessing steps from parameters.
Pipeline helpers#
Utility functions for pipeline optimization.
- spectoprep.pipeline.utils.choose_nearest(value, allowed)[source]#
Choose the nearest allowed value to the given value.
- spectoprep.pipeline.utils.is_valid_pipeline(pipeline, incompatible_sets)[source]#
Check if a pipeline configuration is valid based on incompatibility rules.
- spectoprep.pipeline.utils.generate_pipeline_configurations(preprocessing_steps, incompatible_sets, max_length=5, allowed_lengths=None)[source]#
Generate all valid pipeline configurations based on available steps and incompatibility rules.
- Parameters:
preprocessing_steps (list[str]) – List of available preprocessing step names
incompatible_sets (list[list[str]]) – List of sets, each containing mutually incompatible steps
max_length (int) – Maximum number of steps in a pipeline
allowed_lengths (int | list[int] | tuple[int, ...] | None) – Specific pipeline lengths to allow (e.g., [1, 2])
- Returns:
List of tuples, each representing a valid pipeline configuration
- Return type: