search¶
Hyperparameter search utilities.
Experimental
This module is experimental and may change in future versions.
search
¶
@module: sce.search @depends: numpy, pandas, sklearn, sce.model_presets, sce.models @exports: FeatureCombinationSearch, SearchResult, SearchSummary, train_model @paper_ref: Section 4.3 Model Selection @data_flow: feature subsets -> model presets -> trained estimators -> ranked search results @status: EXPERIMENTAL - Test coverage 21%. Not recommended for production use.
Random search over feature combinations with multiple model configurations. Implements the comprehensive search from sce_analysis.py with cross-fitting.
⚠️ WARNING: This module has minimal test coverage (21%) and is considered experimental. Use at your own risk. Core SCE functionality in engine.py and stats.py is fully tested and production-ready.
SearchResult
dataclass
¶
Result from a single model configuration.
eval_set records which holdout produced the headline metrics:
candidate configurations are scored on the internal validation split
(eval_set="validation"), while the selected winners are refit on the
full training data and scored exactly once on the test set
(eval_set="test"). For test-evaluated results, val_rmse/val_r2/
val_mae preserve the validation score that drove the selection.
Source code in sce/search.py
SearchSummary
dataclass
¶
Summary of model search results.
all_results holds every candidate scored on the validation split.
best_by_rmse, best_by_r2 and baseline_result are selected on
validation metrics, refit on the full training data, and carry unbiased
test-set metrics (eval_set="test").
Source code in sce/search.py
FeatureCombinationSearch
¶
Search over random feature combinations.
Implements the sampling strategy from sce_analysis.py: - Sample 5% of all 2^n combinations (min=50, max=500) - Test with multiple model configurations - Track feature importance across all models
Strategies tested: - baseline: base features only - context_only: SCE features only (random subsets) - context_only_all: ALL SCE features - base_context: base + random SCE subsets - base_context_all: base + ALL SCE features - base_context_sig_lm: base + LM p-value significant SCE features - base_context_sig_tree: base + tree-importance significant SCE features - ablation_remove_best: all features, iteratively remove most important - ablation_remove_worst: all features, iteratively remove least important
Source code in sce/search.py
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 211 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 382 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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | |
__init__
¶
__init__(base_features: List[str], context_features: List[str], sampling_pct: float = 5.0, min_samples: int = 50, max_samples: int = 500, model_configs: List[str] = None, model_params: Optional[Dict[str, Dict[str, Any]]] = None, model_type: str = 'xgboost', random_state: int = 42, run_ablation: bool = True, run_significance_selection: bool = True, p_threshold: float = 0.1, val_fraction: float = 0.2, val_strategy: str = 'random')
Initialize search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_features
|
List[str]
|
Traditional features (always included in base+context) |
required |
context_features
|
List[str]
|
SCE features to sample combinations from |
required |
sampling_pct
|
float
|
Percentage of 2^n combinations to sample |
5.0
|
min_samples
|
int
|
Minimum number of configurations to test |
50
|
max_samples
|
int
|
Maximum number of configurations to test |
500
|
model_configs
|
List[str]
|
List of model config names to test |
None
|
model_type
|
str
|
Supported downstream model type. |
'xgboost'
|
random_state
|
int
|
Random seed for reproducibility |
42
|
run_ablation
|
bool
|
Whether to run ablation experiments (remove best/worst) |
True
|
run_significance_selection
|
bool
|
Whether to run LM/tree significance selection |
True
|
p_threshold
|
float
|
P-value threshold for LM significance selection |
0.1
|
val_fraction
|
float
|
Fraction of the training rows held out as the internal validation split used for candidate selection |
0.2
|
val_strategy
|
str
|
"random" for a shuffled validation split, "tail" to hold out the last rows in the given order (use with time-ordered training data to avoid temporal leakage) |
'random'
|
Source code in sce/search.py
search
¶
search(X_train: DataFrame, y_train: Series, X_test: DataFrame, y_test: Series, progress_callback: Optional[Callable[[int, int], None]] = None) -> SearchSummary
Run the feature combination search.
Evaluation protocol: the training data is split into an internal
fit/validation partition (see val_fraction/val_strategy).
Every candidate combination is scored on the validation split only.
The winners (best by validation RMSE, best by validation R2, and the
best baseline) are then refit on the full training data and evaluated
exactly once on the test set, so reported test metrics are free of
selection bias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_train
|
DataFrame
|
Training features |
required |
y_train
|
Series
|
Training targets |
required |
X_test
|
DataFrame
|
Test features (used only for the final evaluation) |
required |
y_test
|
Series
|
Test targets (used only for the final evaluation) |
required |
progress_callback: Optional callback(current, total) for progress
Returns:
| Type | Description |
|---|---|
SearchSummary
|
SearchSummary with validation-scored candidates and test-scored winners |
Source code in sce/search.py
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 382 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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
get_aggregated_importance
¶
Get feature importance aggregated across all models.
Source code in sce/search.py
train_model
¶
train_model(X_train: DataFrame, y_train: Series, X_test: DataFrame, y_test: Series, model_type: str = 'xgboost', config_name: str = 'default', model_params: Optional[Dict[str, Dict[str, Any]]] = None) -> Tuple[Any, Dict[str, float], pd.DataFrame]
Train a model and compute metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_train
|
DataFrame
|
Training features |
required |
y_train
|
Series
|
Training targets |
required |
X_test
|
DataFrame
|
Test features |
required |
y_test
|
Series
|
Test targets |
required |
model_type
|
str
|
Supported downstream model type. |
'xgboost'
|
config_name
|
str
|
Model configuration name |
'default'
|
Returns:
| Type | Description |
|---|---|
Tuple[Any, Dict[str, float], DataFrame]
|
Tuple of (model, metrics_dict, feature_importance_df) |