Feature Selection Lasso#
lasso
#
| CLASS | DESCRIPTION |
|---|---|
Lasso |
Lasso regression with cross-validation for feature selection. |
| FUNCTION | DESCRIPTION |
|---|---|
lasso |
Lasso cross-validation with feature selection analysis. |
lasso_stability_selection |
Stability selection for robust feature selection using bootstrap sampling. |
lasso_selection_path |
Compute the full regularization path for feature selection analysis. |
Classes#
Lasso(n_cv: int = 5, alphas: torch.Tensor | None = None, max_iter: int = 1000, tol: float = 0.0001, eps: float = 0.001, selection: Literal['cyclic', 'random'] = 'cyclic', n_jobs: int = -1, threshold: float = 1e-08, random_state: int | None = None, verbose: bool | int = False)
#
Bases: Estimator
Lasso regression with cross-validation for feature selection.
Implements L1-regularized linear regression using scikit-learn's LassoCV or MultiTaskLassoCV for single and multi-target regression respectively.
Note: computation uses sklearn (CPU-only). Input tensors are converted to CPU arrays for computation, then results are returned as tensors.
| PARAMETER | DESCRIPTION |
|---|---|
n_cv
|
Number of folds for cross-validation.
TYPE:
|
alphas
|
Grid of alpha values for cross-validation. If None, automatically generated.
TYPE:
|
max_iter
|
Maximum number of iterations for coordinate descent.
TYPE:
|
tol
|
Tolerance for optimization convergence.
TYPE:
|
eps
|
Length of regularization path (ratio of min/max alpha).
TYPE:
|
selection
|
Feature selection strategy for coordinate descent.
TYPE:
|
n_jobs
|
Number of parallel jobs for cross-validation. -1 uses all processors.
TYPE:
|
threshold
|
Threshold for feature selection.
TYPE:
|
random_state
|
Random seed for reproducibility.
TYPE:
|
verbose
|
Verbosity level for training progress.
TYPE:
|
Properties
coefficients : torch.Tensor Learned coefficients of the model.
bias : torch.Tensor Learned bias/intercept of the model.
alphas : torch.Tensor Optimal alpha value selected from cross-validation.
lasso_model : LassoCV or MultiTaskLassoCV Underlying sklearn Lasso model.
scores : torch.Tensor or None Cross-validation scores for each alpha value.
features : torch.Tensor Indices of selected features with non-zero coefficients.
metrics : dict[str, torch.Tensor] Dictionary containing metrics (coefficients, bias, alphas, scores, features).
| METHOD | DESCRIPTION |
|---|---|
fit |
Fit Lasso model to data. |
predict |
Predict using fitted coefficients. |
score |
Compute coefficient of determination on the given test data and labels. |
transform |
Transform data by selecting features with non-zero coefficients. |
fit_transform |
Fit Lasso and transform data in one step. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
features |
Indices of selected features with non-zero coefficients.
TYPE:
|
metrics |
Get all metrics from fitted model.
TYPE:
|
coefficients |
Get coefficients from fitted Lasso model.
TYPE:
|
bias |
Get bias/intercept from fitted Lasso model.
TYPE:
|
alpha |
Get optimal alpha value from fitted Lasso model.
TYPE:
|
lasso_model |
Get the underlying sklearn Lasso model.
TYPE:
|
scores |
Get cross-validation scores from fitted Lasso model.
TYPE:
|
Source code in spectre/feature_selection/lasso.py
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 | |
Attributes#
features: torch.Tensor
property
#
Indices of selected features with non-zero coefficients.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Indices of features with non-zero coefficients of shape (n_selected, ). |
metrics: dict[str, torch.Tensor]
property
#
Get all metrics from fitted model.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Dictionary containing:
|
coefficients: torch.Tensor
property
#
Get coefficients from fitted Lasso model.
bias: torch.Tensor
property
#
Get bias/intercept from fitted Lasso model.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Learned bias/intercept value. |
alpha: torch.Tensor
property
#
Get optimal alpha value from fitted Lasso model.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Optimal alpha value selected by cross-validation. |
lasso_model: LassoCV | MultiTaskLassoCV
property
#
Get the underlying sklearn Lasso model.
| RETURNS | DESCRIPTION |
|---|---|
LassoCV or MultiTaskLassoCV
|
Fitted sklearn Lasso model. |
scores: torch.Tensor | None
property
#
Get cross-validation scores from fitted Lasso model.
Functions#
fit(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> Lasso
#
Fit Lasso model to data.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input features of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values of shape (n_samples, ) or (n_samples, n_targets).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Lasso
|
Fitted Lasso instance. |
Source code in spectre/feature_selection/lasso.py
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 | |
predict(X: torch.Tensor) -> torch.Tensor
#
Predict using fitted coefficients.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Predictions. |
Source code in spectre/feature_selection/lasso.py
score(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Compute coefficient of determination on the given test data and labels.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Test samples.
TYPE:
|
weights
|
Sample weights.
TYPE:
|
target
|
True values for X.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Coefficient of determination. |
Source code in spectre/feature_selection/lasso.py
transform(X: torch.Tensor) -> torch.Tensor
#
Transform data by selecting features with non-zero coefficients.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Transformed data with selected features of shape (n_samples, n_selected). |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If model has not been fitted yet. |
Source code in spectre/feature_selection/lasso.py
fit_transform(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Fit Lasso 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 of shape (n_samples, ) or (n_samples, n_targets).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Transformed data with selected features of shape (n_samples, n_selected). |
Source code in spectre/feature_selection/lasso.py
Functions#
lasso(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None, n_cv: int = 5, alphas: torch.Tensor | None = None, max_iter: int = 1000, tol: float = 0.0001, eps: float = 0.001, selection: Literal['cyclic', 'random'] = 'cyclic', n_jobs: int | None = None, threshold: float = 1e-08, random_state: int | None = None, verbose: bool | int = False) -> dict[str, torch.Tensor]
#
Lasso cross-validation with feature selection analysis.
Performs multi-task Lasso model training with L1/L2 mixed-norm regularization and provides feature selection statistics, stability analysis, and model interpretation tools.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training features matrix.
TYPE:
|
weights
|
Sample weights for training.
TYPE:
|
target
|
Target values.
TYPE:
|
n_cv
|
Number of folds for cross-validation.
TYPE:
|
alphas
|
Grid of alpha values for cross-validation. If None, automatically generated.
TYPE:
|
max_iter
|
Maximum number of iterations for coordinate descent.
TYPE:
|
tol
|
Tolerance for optimization convergence.
TYPE:
|
eps
|
Length of regularization path (ratio of min/max alpha).
TYPE:
|
selection
|
Feature selection strategy for coordinate descent.
TYPE:
|
n_jobs
|
Number of parallel jobs for cross-validation.
TYPE:
|
threshold
|
Threshold for feature selection.
TYPE:
|
random_state
|
Random seed for reproducibility.
TYPE:
|
verbose
|
Verbosity level for training progress.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Dictionary containing:
|
Source code in spectre/feature_selection/lasso.py
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 | |
lasso_stability_selection(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None, n_cv: int = 5, max_iter: int = 1000, tol: float = 0.0001, eps: float = 0.001, n_bootstrap: int = 100, subsample_ratio: float = 0.8, alpha_grid: torch.Tensor | None = None, n_alphas: int = 20, threshold_prob: float = 0.6, threshold: float = 0.1, random_state: int | None = None, verbose: bool = False) -> dict
#
Stability selection for robust feature selection using bootstrap sampling.
Performs feature selection across multiple bootstrap samples to identify features that are consistently selected, providing more robust and interpretable feature selection than single-model approaches.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training features matrix.
TYPE:
|
weights
|
Sample weights.
TYPE:
|
target
|
Target values.
TYPE:
|
n_cv
|
Number of cross-validation splits.
TYPE:
|
max_iter
|
Maximum iterations for Lasso solver.
TYPE:
|
tol
|
Tolerance for convergence.
TYPE:
|
eps
|
Length of regularization path.
TYPE:
|
n_bootstrap
|
Number of bootstrap samples.
TYPE:
|
subsample_ratio
|
Fraction of samples to use in each bootstrap.
TYPE:
|
alpha_grid
|
Grid of alpha values to try, If None, generated automatically.
TYPE:
|
n_alphas
|
Number of alphas in automatic grid.
TYPE:
|
threshold_prob
|
Selection probability threshold_prob for stable features.
TYPE:
|
threshold
|
Coefficient threshold for stable features.
TYPE:
|
random_state
|
Random seed for reproducibility.
TYPE:
|
verbose
|
Whether to show progress.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Dictionary containing:
|
Examples:
>>> import torch
>>> from spectre.feature_selection import lasso_stability_selection
>>>
>>> X = torch.randn(200, 100)
>>> target = torch.randn(200)
>>>
>>> stability_results = lasso_stability_selection(X, target=target, n_bootstrap=50)
Source code in spectre/feature_selection/lasso.py
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 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |
lasso_selection_path(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None, alpha_grid: torch.Tensor | None = None, standardize: bool = True, threshold: float = 0.1, n_cv: int = 5, max_iter: int = 1000, tol: float = 0.0001, eps: float = 0.001, n_alphas: int = 20) -> dict
#
Compute the full regularization path for feature selection analysis.
Traces how feature selection changes across different regularization strengths, providing insights into feature importance hierarchy and selection stability.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training features matrix.
TYPE:
|
weights
|
Sample weights.
TYPE:
|
target
|
Target values.
TYPE:
|
alpha_grid
|
Grid of alpha values. If None, generated automatically.
TYPE:
|
n_alphas
|
Number of alphas in automatic grid.
TYPE:
|
standardize
|
Whether to standardize features.
TYPE:
|
threshold
|
Threshold for feature selection.
TYPE:
|
n_cv
|
Number of cross-validation splits.
TYPE:
|
max_iter
|
Maximum iterations for Lasso solver.
TYPE:
|
tol
|
Tolerance for convergence.
TYPE:
|
eps
|
Length of regularization path.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Dictionary containing:
|
Source code in spectre/feature_selection/lasso.py
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 | |