mojolearn.SelectKBest#

class mojolearn.SelectKBest(score_func=<function f_classif>, *, k=10)#

Select the features with the k highest scores.

Follows scikit-learn’s SelectKBest. Fit calls score_func(X, y) and keeps the k columns with the highest scores. NaN scores rank lowest. Among tied scores the later column is kept, as with scikit-learn’s stable sort. score_func may be f_classif(), chi2(), f_regression(), or any callable returning scores or a (scores, pvalues) pair.

Parameters:
  • score_func (callable, default f_classif) – Function of (X, y) returning the scores.

  • k (int or ‘all’, default 10) – Number of columns to keep, 0 <= k <= n_features (ValueError otherwise), or 'all'.

Variables:
  • scores_ (array of shape (n_features,)) – Score of each column.

  • pvalues_ (array of shape (n_features,) or None) – p-value of each column, when score_func returns them.

  • 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, 20), dtype=np.float32)
>>> y = (X[:, 3] > 0.5).astype(np.int64)
>>> Xt = mojolearn.SelectKBest(mojolearn.f_classif, k=5).fit_transform(X, y)

See also

User guide: Preprocessing

Choosing an estimator

Methods

fit(X[, y])

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.

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.