API Reference / features / selection /

random_subset.py

Random Subset feature selection.

This module provides random feature selection, which randomly selects a subset of features. Useful for ensemble methods and feature bagging.

Classes

RandomSubsetSelector

class features.selection.random_subset.RandomSubsetSelector(FeatureSelector, SelectorMixin)

Randomly select a subset of features.

Chooses a random subset of features, either an absolute number or a percentage. Useful for ensemble methods, feature bagging, and reducing dimensionality when labels are unavailable.
Constructor
__init__(
    self,
    n_features: Union[int, float] = 0.5,
    invert: bool = False,
    random_state: Optional[int] = None,
)

Overview

This selector samples a set of feature indices without replacement. It is a purely stochastic method and does not use any information from the data values or target labels during selection.

Parameters

n_features
int or float = 0.5

Number of features to select:

  • If int >= 1: absolute number of features.
  • If float < 1: fraction of total features.
invert
bool = False
If True, randomly removes features instead of selecting them (retains all features NOT in the random sample).
random_state
int
Random seed for reproducibility.

Attributes

selected_features_
np.ndarray
Indices of the randomly selected features.
n_features_selected_
int
Total number of features selected.

Notes

When to use:
  • For creating diverse ensembles via feature bagging.
  • To benchmark other feature selection methods against a random baseline.
  • When you want to reduce dimensionality quickly without any statistical assumptions.
Limitations:
  • Highly likely to keep irrelevant or redundant features.
  • Results depend entirely on the random seed.

Randomly select half of the features:

python
>>> from tuiml.features.selection import RandomSubsetSelector
>>> import numpy as np
>>> X = np.random.randn(10, 20)
>>> selector = RandomSubsetSelector(n_features=0.5, random_state=42)
>>> X_new = selector.fit_transform(X)
>>> print(X_new.shape[1])
10

Methods

fit (self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'RandomSubsetSelector'

Fit the random subset selector.

Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
Ignored
Not used, present for API consistency. RandomSubsetSelector is unsupervised.
Returns
self
RandomSubsetSelector
The fitted selector.
transform (self, X: np.ndarray) -> np.ndarray

Transform X by keeping only the randomly selected features.

Parameters
X
ndarray of shape (n_samples, n_features)
Input data.
Returns
X_new
ndarray of shape (n_samples, n_selected_features)
Data with selected features.
get_parameter_schema (cls) -> Dict[str, Any]

Return JSON Schema for constructor parameters.

BootstrapFeaturesSelector

class features.selection.random_subset.BootstrapFeaturesSelector(FeatureSelector, SelectorMixin)

Bootstrap feature selection for ensemble methods.

Selects features using bootstrap sampling (sampling with replacement to create a sample of the same size, then taking unique indices), commonly used in Random Forest and other bagging-based ensembles.
Constructor
__init__(
    self,
    n_features: Union[int, float, str] = 'sqrt',
    random_state: Optional[int] = None,
)

Overview

Bootstrap feature selection introduces diversity by allowing some features to be sampled multiple times while others are omitted in a single draw. This increases the randomness and robustness of ensemble models.

Parameters

n_features
int, float, or {"sqrt", "log2"} = "sqrt"

Number of features to draw in the bootstrap sample:

  • int >= 1: absolute number.
  • float < 1: fraction of total.
  • "sqrt": :math:`\sqrt{n_{features}}`.
  • "log2": :math:`log_2(n_{features})`.
random_state
int
Random seed for reproducibility.

Attributes

selected_features_
np.ndarray
Unique indices selected during the bootstrap process.
n_features_selected_
int
Actual number of unique features selected.

Notes

When to use:
  • Specifically for ensemble methods like Random Forest.
  • When you want to simulate feature bagging.

Select square root of features using bootstrap:

python
>>> from tuiml.features.selection import BootstrapFeaturesSelector
>>> import numpy as np
>>> X = np.random.randn(10, 100)
>>> selector = BootstrapFeaturesSelector(n_features="sqrt", random_state=42)
>>> X_new = selector.fit_transform(X)
>>> print(X_new.shape[1])
10

Methods

fit (self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'BootstrapFeaturesSelector'

Fit the bootstrap feature selector.

Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
Ignored
Not used.
Returns
self
BootstrapFeaturesSelector
The fitted selector.
transform (self, X: np.ndarray) -> np.ndarray

Transform X by keeping only the bootstrap-sampled features.

Parameters
X
ndarray of shape (n_samples, n_features)
Input data.
Returns
X_new
ndarray of shape (n_samples, n_selected_features)
Data with only bootstrap-selected features.
get_parameter_schema (cls) -> Dict[str, Any]

Return JSON Schema for constructor parameters.