Feature Selection Sequential#
sequential
#
| CLASS | DESCRIPTION |
|---|---|
SequentialFeatureSelector |
Sequential feature selection with optional floating variant. |
Classes#
SequentialFeatureSelector(estimator: Any, score_fn: Callable[[Any], torch.Tensor], out_features: int = 0, forward: bool = True, floating: bool = False, labels: list[str] | None = None, n_cv: int | Any = 5, n_jobs: int = -1, maximize: bool = True, random_state: int | None = None)
#
Sequential feature selection with optional floating variant.
Implements forward and backward sequential feature selection algorithms with optional floating search to escape local optima. Uses cross-validation to evaluate feature subsets.
| PARAMETER | DESCRIPTION |
|---|---|
estimator
|
Estimator with
TYPE:
|
score_fn
|
User-defined score function with signature:
TYPE:
|
out_features
|
Target number of features to select. Must be positive and not exceed total number of features in data.
TYPE:
|
forward
|
If True, performs forward selection (starts empty, adds features). If False, performs backward elimination (starts full, removes features).
TYPE:
|
floating
|
If True, enables floating variant that conditionally removes (forward) or adds (backward) features after each step to escape local optima. Note: Progress bar may not accurately reflect progress with floating enabled due to variable iteration counts.
TYPE:
|
labels
|
Optional feature names. If provided, must have one name per feature.
TYPE:
|
n_cv
|
Cross-validation strategy. If int, number of folds for KFold with shuffle.
Otherwise, any sklearn splitter object with a
TYPE:
|
n_jobs
|
Number of parallel jobs for cross-validation. -1 uses all processors.
TYPE:
|
maximize
|
If True, higher scores are better. If False, lower scores are better.
TYPE:
|
random_state
|
Random seed for cross-validation shuffle.
TYPE:
|
Properties
features : tuple[int] | None Indices of selected features after fitting.
scores : torch.Tensor | None Cross-validation scores for selected feature subset of shape (n_cv, ).
labels : list[str] | None
Names of selected features if labels was provided.
metrics : dict[str, float] | None Dictionary of selected feature subset metrics.
| METHOD | DESCRIPTION |
|---|---|
check_fitted |
Check if the estimator is fitted and raise an error if not. |
score |
Compute cross-validation score for feature subset. |
select |
Select best feature to add (forward) or remove (backward). |
fit |
Fit selector to find optimal feature subset. |
transform |
Transform data by selecting best features. |
fit_transform |
Fit selector and transform data in one step. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
features |
Indices of selected features after fitting.
TYPE:
|
scores |
Cross-validation scores for selected feature subset.
TYPE:
|
labels |
Names of selected features if labels was provided.
TYPE:
|
metrics |
Dictionary of selected feature metrics.
TYPE:
|
Source code in spectre/feature_selection/sequential.py
Attributes#
features: tuple[int, ...]
property
#
Indices of selected features after fitting.
| RETURNS | DESCRIPTION |
|---|---|
tuple[int, ...]
|
Indices of selected features (out_features, ). |
scores: torch.Tensor
property
#
Cross-validation scores for selected feature subset.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
CV fold scores of shape (n_splits, ) where n_splits is determined by the CV splitter (n_cv parameter). |
labels: list[str] | None
property
#
Names of selected features if labels was provided.
| RETURNS | DESCRIPTION |
|---|---|
list[str] | None
|
List of selected feature names (out_features, ), or None if labels not provided. |
metrics: dict[str, float]
property
#
Dictionary of selected feature metrics.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, float]
|
Dictionary of selected feature metrics. |
Functions#
check_fitted() -> None
#
Check if the estimator is fitted and raise an error if not.
score(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> tuple[torch.Tensor, torch.Tensor]
#
Compute cross-validation score for feature subset.
Fits estimator on each train fold and evaluates using score_fn.
Uses K-fold cross-validation with internal parallelization disabled
(n_jobs=1) to avoid nested parallelism conflicts with feature-level
parallelization in select().
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data with selected features of shape (n_samples, out_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values for supervised methods. Currently unused but kept for API extensibility.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
Mean CV score (scalar) and individual fold scores (n_cv, ). |
Source code in spectre/feature_selection/sequential.py
select(*, X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None, search_set: set[int], fixed_set: set[int], forward: bool = True) -> tuple[tuple[int, ...], torch.Tensor, torch.Tensor]
#
Select best feature to add (forward) or remove (backward).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values for supervised methods of shape (n_samples, ).
TYPE:
|
search_set
|
Set of feature indices to search from.
TYPE:
|
fixed_set
|
Set of feature indices that must be included.
TYPE:
|
forward
|
If True, adds one feature from remaining. If False, removes one.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[tuple[int, ...], Tensor, Tensor]
|
Best feature subset (indices), its mean score, and CV fold scores. |
Source code in spectre/feature_selection/sequential.py
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 | |
fit(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> SequentialFeatureSelector
#
Fit selector to find optimal feature subset.
Performs sequential feature selection using forward/backward search with optional floating to escape local optima.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values for supervised methods of shape (n_samples, ).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SequentialFeatureSelector
|
Returns self for method chaining. |
Source code in spectre/feature_selection/sequential.py
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 666 667 668 669 670 671 672 673 674 675 676 677 678 | |
transform(X: torch.Tensor) -> torch.Tensor
#
Transform data by selecting best features.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Transformed data with selected features of shape (n_samples, out_features). |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If selector has not been fitted yet. |
Source code in spectre/feature_selection/sequential.py
fit_transform(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Fit selector and transform data in one step.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values for supervised methods of shape (n_samples, ). Not used.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Transformed data with selected features of shape (n_samples, out_features). |