AlgorithmWrapper#
- class AlgorithmWrapper(name: str, display_name: str, algorithm_class: Type, default_params: Dict[str, Any] | None = None, hyperparam_grid: Dict[str, Any] | None = None)[source]#
A wrapper class for machine learning algorithms.
Provides methods to instantiate models with default or tuned parameters and manages hyperparameter grids for model tuning. This class serves as a bridge between Brisk’s configuration system and scikit-learn algorithms, enabling consistent parameter management and model instantiation.
- Parameters:
- namestr
Unique identifier for the algorithm used in configurations
- display_namestr
Human-readable name for display purposes in reports and UI
- algorithm_classType
The scikit-learn algorithm class to be instantiated
- default_paramsdict, optional
Default parameters for model instantiation, by default None
- hyperparam_griddict, optional
Grid of parameters for hyperparameter tuning, by default None
- Attributes:
- namestr
Algorithm identifier used in configurations
- display_namestr
Human-readable name for display purposes
- algorithm_classType
The scikit-learn algorithm class
- default_paramsdict
Default parameters for model instantiation
- hyperparam_griddict
Hyperparameter grid for tuning
- Raises:
- TypeError
If name, display_name, default_params, or hyperparam_grid are not of the expected types
- ValueError
If algorithm_class is not from scikit-learn
Notes
The AlgorithmWrapper class enforces that all algorithm classes must be from scikit-learn to ensure compatibility with Brisk’s evaluation and reporting systems.
Examples
- Create a wrapper for Linear Regression:
>>> from sklearn.linear_model import LinearRegression >>> wrapper = AlgorithmWrapper( ... name="ridge", ... display_name="Ridge Regression", ... algorithm_class=Ridge, ... default_params={"fit_intercept": True}, ... hyperparam_grid={"alpha": range(0, 1, 0.1)} ... )
- Instantiate a model:
>>> model = wrapper.instantiate()
- export_config() Dict[str, Any][source]#
Export this AlgorithmWrapper’s configuration for rerun functionality.
Creates a serialized configuration dictionary that can be used to recreate this AlgorithmWrapper instance. Handles complex objects like scikit-learn estimators by serializing their parameters.
- Returns:
- Dict[str, Any]
Configuration dictionary that can be used to recreate this AlgorithmWrapper instance
Notes
The exported configuration includes: - Algorithm name and display name - Algorithm class module and name - Serialized default parameters - Serialized hyperparameter grid
Complex objects like scikit-learn estimators are serialized with their module, class name, and parameters for proper reconstruction.
Examples
- Export configuration for saving:
>>> config = wrapper.export_config() >>> # Save config to file or database
- get_hyperparam_grid() Dict[str, Any][source]#
Get the hyperparameter grid.
Returns the current hyperparameter grid dictionary used for hyperparameter tuning.
- Returns:
- Dict[str, Any]
Current hyperparameter grid dictionary
Examples
- Get hyperparameter grid for tuning:
>>> grid = wrapper.get_hyperparam_grid() >>> print(grid) >>> {"C": [0.1, 1.0, 10.0], "gamma": ["scale", "auto"]}
- instantiate() Any[source]#
Instantiate model with default parameters.
Creates a new instance of the algorithm class using the current default parameters. Adds a wrapper_name attribute to the model for identification purposes.
- Returns:
- Any
Model instance with default parameters and wrapper_name attribute
Notes
The instantiated model will have a ‘wrapper_name’ attribute set to the algorithm’s name, which can be useful for identification in evaluation and reporting contexts.
Examples
- Create and instantiate a model:
>>> wrapper = AlgorithmWrapper( ... name="linear", ... display_name="Linear Regression", ... algorithm_class=LinearRegression ... ) >>> model = wrapper.instantiate() >>> print(model.wrapper_name) # "linear"
- instantiate_tuned(best_params: Dict[str, Any]) Any[source]#
Instantiate model with tuned parameters.
Creates a new instance of the algorithm class using tuned hyperparameters. Automatically includes any default parameters that were not part of the tuning process.
- Parameters:
- best_paramsdict
Tuned hyperparameters from a hyperparameter search process
- Returns:
- Any
Model instance with tuned parameters and wrapper_name attribute
- Raises:
- TypeError
If best_params is not a dictionary
Notes
If a parameter is set in default_params but not in hyperparam_grid, the default value will be preserved in the tuned parameters. This ensures that all necessary parameters are included in the final model.
Examples
- Instantiate with tuned parameters:
>>> best_params = {"C": 1.0, "gamma": "scale"} >>> model = wrapper.instantiate_tuned(best_params)
- to_markdown() str[source]#
Create markdown representation of algorithm configuration.
Generates a formatted markdown string containing the algorithm’s name, class, default parameters, and hyperparameter grid. Useful for documentation and reporting purposes.
- Returns:
- str
Markdown formatted string containing algorithm information
Notes
The markdown output includes: - Algorithm display name and identifier - Algorithm class name - Formatted default parameters code block - Formatted hyperparameter grid code block
Examples
- Generate markdown documentation:
>>> md = wrapper.to_markdown() >>> print(md)