Skip to Content
OchreML
Utilities & Preprocessing

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

ExportKindDescription.dit Tag
VarianceThresholdFilter transformerDrops features whose variance is <= threshold96
SelectKBestFilter transformerKeeps the k highest-scoring features97
SelectPercentileFilter transformerKeeps the top percentile percent of features98
f_regressionScoring functionUnivariate linear regression F-statistics and p-valuesN/A
f_classifScoring functionOne-way ANOVA F-statistics and p-valuesN/A
chi2Scoring functionChi-squared statistics and p-values for non-negative featuresN/A
RFEMeta-selectorRecursive Feature Elimination using coef_ or feature_importances_N/A
SelectFromModelMeta-selectorSelects features whose importance exceeds a thresholdN/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)
ParameterTypeDefaultDescription
Xarray-like (n_samples, n_features)requiredFeature matrix.
yarray-like (n_samples,)requiredContinuous target vector.
centerboolTrueKeyword-only; center X and y before computing correlation.
force_finiteboolTrueKeyword-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

ParameterTypeDefaultDescription
score_funcCallablef_classifScoring function (f_classif, f_regression, chi2, or custom callable).
kint or "all"10Number of top features to keep (or "all" to bypass selection).
devicestr | NoneNoneTarget 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

ParameterTypeDefaultDescription
score_funcCallablef_classifScoring function (f_classif, f_regression, chi2, or custom callable).
percentileint | float10Percentage of features to keep in [0, 100].
devicestr | NoneNoneTarget 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

ParameterTypeDefaultDescription
estimatorsupervised estimatorrequiredEstimator exposing coef_ or feature_importances_ after fit.
n_features_to_selectint | float | NoneNoneTarget count (int), fraction in (0, 1] (float), or None (half of features).
stepint | float1Number (int) or fraction in (0, 1) (float) of features removed per iteration.
verboseint0Verbosity 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_ (1 for 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

ParameterTypeDefaultDescription
estimatorestimatorrequiredBase estimator exposing coef_ or feature_importances_.
thresholdstr | float | int | NoneNoneKeyword-only; numeric threshold, "mean", "median", scaling expression like "1.25*mean", or None (defaults to mean importance).
prefitboolFalseKeyword-only; if True, uses estimator directly without re-fitting.
norm_orderint1Keyword-only; norm order used to aggregate 2D coef_ arrays across classes/outputs.
max_featuresint | NoneNoneKeyword-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_

On this page