Optimization
Hyperparameter Optimization (payn.Optimization.Optimization)
The payn.Optimization module provides a unified interface for hyperparameter tuning, supporting both CatBoostClassifier and CatBoostRegressor architectures. It ensures consistent model evaluation by wrapping the training logic and enforcing reproducibility constraints across different optimization strategies.
- Search Space: The search space is dynamically constructed based on the user configuration (
config.yaml). The module supports a wide range of CatBoost parameters, includinglearning_rate(log-uniform distribution),depth(integer),iterations(integer), and multiple additional structural parameters (grow_policy,subsample,colsample_bylevel,min_data_in_leaf,one_hot_max_size,max_bin, andl2_leaf_reg). By default, in this workdepthandlearning_ratewere optimized with an early stopping within 1000 iterations. - Metric-Aware Directionality: The module automatically maps the chosen evaluation metric (e.g., RMSE, Logloss, F1, MCC) to the appropriate optimization direction (minimize or maximize) using an internal
eval_direction_dict. This ensures that the objective function correctly rewards or penalizes trial outcomes without manual intervention.
Base class for hyperparameter optimization strategies.
Attributes:
| Name | Type | Description |
|---|---|---|
target_model |
Any
|
Instance of the model wrapper (e.g., SpyModel). |
random_state |
int
|
Random seed for reproducibility. |
logger |
Optional[Logger]
|
Logger instance for tracking experiments. |
feature_column_name |
Optional[str]
|
Name of the column containing feature vectors. |
training_target_column_name |
Optional[str]
|
Name of the training target column. |
evaluation_target_column_name |
Optional[str]
|
Name of the evaluation target column. |
optimization_type |
Optional[str]
|
Type of optimization ('Bayesian' or 'Grid'). |
optimisation_iterations |
Optional[int]
|
Number of optimization iterations. |
search_space |
Union[List[str], Dict[str, Any], None]
|
Search space configuration. |
catboost_max_depth |
Optional[int]
|
Maximum depth constraint for CatBoost. |
catboost_max_iterations |
Optional[int]
|
Maximum iterations constraint for CatBoost. |
catboost_min_learning_rate |
Optional[float]
|
Minimum learning rate constraint. |
catboost_max_bin |
Optional[int]
|
Maximum bin size constraint. |
Source code in payn\Optimization\optimization.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
__init__(target_model, random_state, logger=None, feature_column_name=None, training_target_column_name=None, evaluation_target_column_name=None, optimization_type=None, optimisation_iterations=None, search_space=None, catboost_max_depth=None, catboost_max_iterations=None, catboost_min_learning_rate=None, catboost_max_bin=None, **kwargs)
Initialize the Optimization class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_model
|
Any
|
Instance of the model wrapper (e.g., SpyModel). |
required |
random_state
|
int
|
Random seed for reproducibility. |
required |
logger
|
Logger
|
Instance of Logger class for logging purposes. |
None
|
feature_column_name
|
str
|
Column name of target variable. |
None
|
training_target_column_name
|
str
|
Column name of target variable for training data. |
None
|
evaluation_target_column_name
|
str
|
Column name of target variable for evaluation data. |
None
|
optimization_type
|
str
|
Type of optimization to perform (Bayesian or Grid). |
None
|
optimisation_iterations
|
int
|
Number of iterations of optimization. |
None
|
search_space
|
List
|
List of hyperparameters to optimize. |
None
|
catboost_max_depth
|
int
|
Maximum depth of catboost trees. |
None
|
catboost_max_iterations
|
int
|
Maximum number of iterations for catboost. |
None
|
catboost_min_learning_rate
|
float
|
Minimum learning rate for catboost. |
None
|
catboost_max_bin
|
int
|
Maximum bin size for catboost. |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Source code in payn\Optimization\optimization.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
from_config(config, target_model, logger=None, **kwargs)
classmethod
Alternative constructor that extracts the required parameters from a config object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Dict[str, Any]
|
Configuration dictionary. |
required |
target_model
|
Any
|
The model wrapper instance. |
required |
logger
|
Logger
|
Logger instance. |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Optimization |
Optimization
|
An initialized Optimization instance. |
Source code in payn\Optimization\optimization.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
optimize(train_data, val_data, test_data=None, optimization_type=None, **kwargs)
Optimize the model using the specified optimization technique.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_data
|
DataFrame
|
Training dataset. |
required |
val_data
|
DataFrame
|
Validation dataset. |
required |
test_data
|
DataFrame
|
Test dataset. |
None
|
optimization_type
|
str
|
Override for the optimization type. |
None
|
Returns:
| Type | Description |
|---|---|
Union[CatBoostClassifier, CatBoostRegressor]
|
Union[CatBoostClassifier, CatBoostRegressor]: The optimized model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the optimization type is unsupported. |
Source code in payn\Optimization\optimization.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
Bayesian Optimization (payn.Optimization.BayesianOptimization)
This strategy integrates the Optuna framework to perform efficient exploration of high-dimensional hyperparameter spaces.
- Tree-structured Parzen Estimator (TPE): The optimization utilizes a TPE sampler, which models the probability of hyperparameter values given past results. This allows for significantly more efficient traversal of the search space compared to random or grid search methods. In this study, Bayesian Optimization was configured with 50 iterations by default, demonstrating convergence for both SpyModel and Regression tasks.
- Reproducibility: The
TPESampleris explicitly instantiated with a fixed globalrandom_state. This guarantees that the sequence of hyperparameter suggestions is deterministic across experimental runs, eliminating variability caused by the stochastic nature of the sampling process.
Bases: Optimization
Subclass for performing Bayesian Optimization using Optuna.
Source code in payn\Optimization\optimization.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
objective(trial, train_data, val_data, eval_direction, feature_column_name=None, training_target_column_name=None, evaluation_target_column_name=None)
Objective function to optimize CatBoost hyperparameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trial
|
Trial
|
Trial object for suggestions. |
required |
train_data
|
DataFrame
|
Training dataset. |
required |
val_data
|
DataFrame
|
Validation dataset. |
required |
eval_direction
|
str
|
Either 'minimize' or 'maximize', depending on the metric. |
required |
feature_column_name
|
str
|
Override for feature column name. |
None
|
training_target_column_name
|
str
|
Override for training target column name. |
None
|
evaluation_target_column_name
|
str
|
Override for evaluation target column name. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Validation score for current hyperparameters. |
Source code in payn\Optimization\optimization.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
optimize(train_data, val_data, test_data=None, iterations=None)
Perform Bayesian optimization to find the best hyperparameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_data
|
DataFrame
|
Training dataset. |
required |
val_data
|
DataFrame
|
Validation dataset. |
required |
test_data
|
DataFrame
|
Test dataset. |
None
|
iterations
|
int
|
Override for number of trials. |
None
|
Returns:
| Type | Description |
|---|---|
Union[CatBoostClassifier, CatBoostRegressor]
|
CatBoostClassifier or CatBoostRegressor: Trained model with best hyperparameters. |
Source code in payn\Optimization\optimization.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
Grid Optimization (payn.Optimization.GridOptimization)
This strategy implements a combinatorial search over a manually defined grid of hyperparameter values.
- Combinatorial Search Space: The module generates every possible combination of the provided parameter lists using Cartesian products (
itertools.product). This ensures that the global optimum within the defined discrete space is found, provided the grid resolution is sufficient. - Deterministic Execution: Unlike stochastic search methods, Grid Optimization is inherently deterministic.
- Use Case: While computationally more expensive than Bayesian methods for high-dimensional spaces, this strategy serves as a robust baseline for validating the stability of specific hyperparameters (e.g.,
depthorlearning_rate) in isolation.
Bases: Optimization
Subclass for performing Grid Search optimization.
Source code in payn\Optimization\optimization.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
optimize(train_data, val_data, test_data, search_space=None, eval_direction=None, iterations=None, **kwargs)
Perform Grid Search optimization to find the best hyperparameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_data
|
DataFrame
|
Training dataset. |
required |
val_data
|
DataFrame
|
Validation dataset. |
required |
test_data
|
DataFrame
|
Test dataset. |
required |
search_space
|
Dict[str, List[Any]]
|
Override for the search space. |
None
|
eval_direction
|
str
|
Override for evaluation direction. |
None
|
iterations
|
int
|
Override for number of grid combinations to try (if applicable). |
None
|
Returns:
| Type | Description |
|---|---|
Union[CatBoostClassifier, CatBoostRegressor]
|
CatBoostClassifier or CatBoostRegressor: The trained model with the best hyperparameters. |
Source code in payn\Optimization\optimization.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |