mojolearn.SelectKBest#

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

Bases: _SelectorMixin

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)
__init__(score_func=<function f_classif>, *, k=10)[source]#

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)[source]#

Fit to X, then transform X.

Returns:

array – The transformed data.