mojolearn.RFE#

class mojolearn.RFE(estimator, *, n_features_to_select=None, step=1, verbose=0, importance_getter='auto')#

Feature ranking with recursive feature elimination.

Follows scikit-learn’s RFE. The estimator is fitted on the current features, the step weakest are removed, and the process repeats until n_features_to_select remain. Importance is the squared coef_ (summed over rows when 2-D) or feature_importances_. Ties are broken by a stable sort: of two equally weak columns, the lower index is removed first (scikit-learn leaves the order unspecified).

The estimator is cloned with type(estimator)(**estimator.get_params()) and fitted on float32 data. predict, predict_proba, decision_function and score apply the final estimator to the selected columns.

Parameters:
  • estimator (object) – Estimator with fit that exposes coef_ or feature_importances_ (ValueError otherwise, with importance_getter='auto').

  • n_features_to_select (int or float, default None) – Number of features to keep, or a fraction of them when a float in (0, 1). None keeps half.

  • step (int or float, default 1) – Number of features removed per iteration, or a fraction of the features when a float in (0, 1). Must be greater than 0.

  • verbose (int, default 0) – Accepted for scikit-learn compatibility.

  • importance_getter (‘auto’, str or callable, default ‘auto’) – 'auto' uses coef_ or feature_importances_; a dotted attribute path or a callable taking the estimator picks the importances instead (they are squared).

Variables:
  • estimator_ (object) – Estimator fitted on the selected features.

  • n_features_ (int) – Number of selected features.

  • support_ (list of bool) – Mask of selected features.

  • ranking_ (array of int64) – Rank of each feature; selected features have rank 1.

  • n_features_in_ (int) – Number of columns seen in fit.

  • numeric_mode_ (str) – Numeric mode at fit time; transform runs in the same mode.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 10), dtype=np.float32)
>>> y = (X[:, 0] + X[:, 1] > 1).astype(np.int64)
>>> rfe = mojolearn.RFE(mojolearn.LinearDiscriminantAnalysis(), n_features_to_select=3).fit(X, y)
>>> rfe.support_

See also

User guide: Preprocessing

Choosing an estimator

Methods

decision_function(X)

fit(X, y, **fit_params)

fit_transform(X[, y])

Fit to X, then transform X.

get_params([deep])

Return the constructor parameters as a dict.

get_support([indices])

Return the selected features.

predict(X)

predict_proba(X)

score(X, y)

set_params(**params)

Set constructor parameters.

transform(X)

Keep only the selected columns of X.

fit_transform(X, y=None, **fit_params)#

Fit to X, then transform X.

Returns:

array – The transformed data.

get_params(deep=True)#

Return the constructor parameters as a dict.

Parameters:

deep (bool, default True) – Accepted for scikit-learn compatibility.

Returns:

dict – Parameter names mapped to their values.

get_support(indices=False)#

Return the selected features.

Parameters:

indices (bool, default False) – Return the indices of the selected columns instead of a boolean mask.

Returns:

list – Boolean mask over the input columns, or the selected column indices.

set_params(**params)#

Set constructor parameters.

An unknown parameter name raises ValueError.

Returns:

self

transform(X)#

Keep only the selected columns of X.

Raises ValueError when no column is selected.

Parameters:

X (array-like of shape (n_samples, n_features)) – Data with the columns seen in fit.

Returns:

array of shape (n_samples, n_selected_features) – The selected columns, float32.