Feature Selection
Rust-backed univariate statistical tests, variance filtering, and model-based feature selectors in ochreml.feature_selection.
ochreml.feature_selection provides Rust-backed statistical scoring functions, filter transformers, and model-based meta-selectors (all lazily importable from ochreml). (^▽^)
Overview
| Export | Kind | Description | .dit Tag |
|---|---|---|---|
VarianceThreshold | Filter transformer | Drops features whose variance is <= threshold | 96 |
SelectKBest | Filter transformer | Keeps the k highest-scoring features | 97 |
SelectPercentile | Filter transformer | Keeps the top percentile percent of features | 98 |
f_regression | Scoring function | Univariate linear regression F-statistics and p-values | N/A |
f_classif | Scoring function | One-way ANOVA F-statistics and p-values | N/A |
chi2 | Scoring function | Chi-squared statistics and p-values for non-negative features | N/A |
RFE | Meta-selector | Recursive Feature Elimination using coef_ or feature_importances_ | N/A |
SelectFromModel | Meta-selector | Selects features whose importance exceeds a threshold | N/A |
Univariate Scoring Functions
All three statistical tests run in Rust and return a (scores, pvalues) tuple of 1D float64 NumPy arrays of length n_features.
f_regression
from ochreml.feature_selection import f_regression
f_stats, p_values = f_regression(X, y, center=True, force_finite=True)| Parameter | Type | Default | Description |
|---|---|---|---|
X | array-like (n_samples, n_features) | required | Feature matrix. |
y | array-like (n_samples,) | required | Continuous target vector. |
center | bool | True | Keyword-only; center X and y before computing correlation. |
force_finite | bool | True | Keyword-only; replace non-finite F-values (e.g., constant features) with finite defaults. |
f_classif
Computes one-way ANOVA F-values and p-values between each feature and discrete class labels y:
from ochreml.feature_selection import f_classif
f_stats, p_values = f_classif(X, y)chi2
Computes chi-squared statistics and p-values between each non-negative feature (e.g., counts or boolean indicators) and discrete class labels y:
from ochreml.feature_selection import chi2
chi2_stats, p_values = chi2(X, y)VarianceThreshold
Unsupervised Rust-backed selector that removes all features whose sample variance does not exceed threshold (defaults to 0.0, removing constant features).
from ochreml.feature_selection import VarianceThreshold
vt = VarianceThreshold(threshold=0.0, device=None)
X_reduced = vt.fit_transform(X)
mask = vt.get_support(indices=False)Parameters, Methods & Attributes
- Parameters:
threshold: float = 0.0,device: str | None = None - Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),inverse_transform(X),get_support(indices=False),get_params(deep=True),set_params(**params),save(path),load(path, device=None),to(device) - Attributes:
variances_,n_features_in_,device_
SelectKBest
Selects the k highest-scoring features according to score_func. Built-in functions (f_classif, f_regression, chi2) execute end-to-end in Rust; custom Python callables returning scores or (scores, pvalues) are also supported.
from ochreml.feature_selection import SelectKBest, f_classif
selector = SelectKBest(score_func=f_classif, k=10, device=None)
X_best = selector.fit_transform(X, y)
idx = selector.get_support(indices=True)Parameters, Methods & Attributes
| Parameter | Type | Default | Description |
|---|---|---|---|
score_func | Callable | f_classif | Scoring function (f_classif, f_regression, chi2, or custom callable). |
k | int or "all" | 10 | Number of top features to keep (or "all" to bypass selection). |
device | str | None | None | Target compute device. |
- Methods:
fit(X, y),transform(X),fit_transform(X, y),inverse_transform(X),get_support(indices=False),get_params(deep=True),set_params(**params),save(path),load(path, device=None),to(device) - Attributes:
scores_,pvalues_,n_features_in_,device_
SelectPercentile
Selects features in the highest percentile percent of scores according to score_func.
from ochreml.feature_selection import SelectPercentile, f_regression
selector = SelectPercentile(score_func=f_regression, percentile=20, device=None)
X_top = selector.fit_transform(X, y)Parameters, Methods & Attributes
| Parameter | Type | Default | Description |
|---|---|---|---|
score_func | Callable | f_classif | Scoring function (f_classif, f_regression, chi2, or custom callable). |
percentile | int | float | 10 | Percentage of features to keep in [0, 100]. |
device | str | None | None | Target compute device. |
- Methods:
fit(X, y),transform(X),fit_transform(X, y),inverse_transform(X),get_support(indices=False),get_params(deep=True),set_params(**params),save(path),load(path, device=None),to(device) - Attributes:
scores_,pvalues_,n_features_in_,device_
RFE
Recursive Feature Elimination meta-selector. Iteratively fits estimator, ranks features by coef_ (L1 norm across classes/outputs) or feature_importances_, prunes the weakest step features per round until n_features_to_select remain, and refits estimator_ on the final subset.
from ochreml.feature_selection import RFE
from ochreml.linear_model import LogisticRegression
rfe = RFE(
estimator=LogisticRegression(max_iter=500),
n_features_to_select=5,
step=1,
verbose=0,
)
X_sel = rfe.fit_transform(X, y)
y_pred = rfe.predict(X_test)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
estimator | supervised estimator | required | Estimator exposing coef_ or feature_importances_ after fit. |
n_features_to_select | int | float | None | None | Target count (int), fraction in (0, 1] (float), or None (half of features). |
step | int | float | 1 | Number (int) or fraction in (0, 1) (float) of features removed per iteration. |
verbose | int | 0 | Verbosity level. |
Methods & Fitted Attributes
- Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),get_support(indices=False),predict(X),predict_proba(X),decision_function(X),score(X, y),get_params(deep=True),set_params(**params) - Attributes:
support_,ranking_(1for selected features),n_features_,estimator_,n_features_in_
SelectFromModel
Meta-transformer that retains features whose importance weights (coef_ or feature_importances_) meet or exceed threshold, optionally capped at max_features.
from ochreml.feature_selection import SelectFromModel
from ochreml.ensemble import RandomForestClassifier
sfm = SelectFromModel(
estimator=RandomForestClassifier(n_estimators=50, random_state=42),
threshold="median",
prefit=False,
norm_order=1,
max_features=None,
)
X_sel = sfm.fit_transform(X, y)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
estimator | estimator | required | Base estimator exposing coef_ or feature_importances_. |
threshold | str | float | int | None | None | Keyword-only; numeric threshold, "mean", "median", scaling expression like "1.25*mean", or None (defaults to mean importance). |
prefit | bool | False | Keyword-only; if True, uses estimator directly without re-fitting. |
norm_order | int | 1 | Keyword-only; norm order used to aggregate 2D coef_ arrays across classes/outputs. |
max_features | int | None | None | Keyword-only; maximum number of features to retain above threshold. |
Methods & Fitted Attributes
- Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),get_support(indices=False),get_params(deep=True),set_params(**params) - Attributes:
estimator_,threshold_,support_,n_features_in_
Feature & Text Extraction
Rust-backed dictionary vectorization, MurmurHash3 feature hashing, and text tokenization/TF-IDF extraction in ochreml.feature_extraction.
Model Selection & Cross-Validation
Rust-backed dataset splitters, cross-validation scoring, and hyperparameter search in ochreml.model_selection.