Feature Selection Prune#
prune
#
| CLASS | DESCRIPTION |
|---|---|
PrunedLinear |
Linear layer with integrated Lasso-based feature selection and pruning. |
| FUNCTION | DESCRIPTION |
|---|---|
prune_model |
Apply structured pruning to an existing model. |
Classes#
PrunedLinear(in_features: int, out_features: int, mask: 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, random_state: int | None = None, threshold: float = 1e-05, bias: bool = True)
#
Bases: Module
Linear layer with integrated Lasso-based feature selection and pruning.
Automatically performs Lasso regression to select important features, applies binary mask to input features, and initializes weights with Lasso coefficients for optimal performance.
Call fit() method to perform automatic Lasso feature selection before
training and prediction.
| PARAMETER | DESCRIPTION |
|---|---|
in_features
|
Number of input features.
TYPE:
|
out_features
|
Number of output features.
TYPE:
|
mask
|
Binary mask of shape (in_features,) where True = keep feature, False = prune. If None, all features are initially selected and Lasso fitting is required.
TYPE:
|
n_cv
|
Number of folds for cross-validation in Lasso.
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:
|
random_state
|
Random seed for reproducibility.
TYPE:
|
threshold
|
Threshold for considering coefficients as non-zero.
TYPE:
|
bias
|
Whether to include bias term.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
mask |
Binary mask of shape (in_features, ) where True = selected features.
TYPE:
|
coefficients |
Lasso coefficients from feature selection.
TYPE:
|
features |
Indices of selected features.
TYPE:
|
lasso_model |
Fitted Lasso model for feature selection.
TYPE:
|
is_fitted |
Whether the Lasso model has been fitted.
TYPE:
|
Examples:
Create layer and fit with data:
>>> layer = PrunedLinear(in_features=100, out_features=1)
>>> layer.fit(X_train, y_train)
>>> output = layer(X_test)
| METHOD | DESCRIPTION |
|---|---|
fit |
Fit Lasso model and update feature mask and weights. |
forward |
Apply pruned linear transformation. |
check_fitted |
Check if model is fitted, raise error if not. |
Source code in spectre/feature_selection/prune.py
Attributes#
features: torch.Tensor
property
#
Get indices of selected features.
coefficients: torch.Tensor
property
#
Get feature importance scores (absolute Lasso coefficients).
mask: torch.Tensor
property
#
Get feature mask.
sparsity_ratio: float
property
#
Get ratio of selected features to total features.
is_fitted: bool
property
#
Whether the Lasso model has been fitted.
Functions#
fit(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> PrunedLinear
#
Fit Lasso model and update feature mask and weights.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training features matrix.
TYPE:
|
weights
|
Sample weights for training.
TYPE:
|
target
|
Target values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PrunedLinear
|
Self for method chaining. |
Source code in spectre/feature_selection/prune.py
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 | |
forward(X: torch.Tensor) -> torch.Tensor
#
Apply pruned linear transformation.
Uses PyTorch's pruning mechanism to zero out non-selected features.
Must call :meth:fit() before using.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input features.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Output after applying pruned linear layer. |
Source code in spectre/feature_selection/prune.py
Functions#
prune_model(model: torch.nn.Module, mask: torch.Tensor, layer_name: str = 'weight', make_permanent: bool = False) -> torch.nn.Module
#
Apply structured pruning to an existing model.
Uses PyTorch's pruning utilities to mask input features based on input selection.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Neural network to prune.
TYPE:
|
mask
|
Binary mask where True = keep feature, False = prune feature.
TYPE:
|
layer_name
|
Parameter name to prune.
TYPE:
|
make_permanent
|
Whether to permanently remove pruned weights.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Module
|
Pruned model with masked features. |
Examples:
Lasso feature selection:
>>> from spectre.feature_selection import lasso, prune_model
>>> results = lasso(X, target=target)
>>> mask = torch.zeros(X.shape[1], dtype=torch.bool)
>>> mask[results["features"]] = True
Apply to existing model: