SelectKBest<T>

Models & Types · AiDotNet.Preprocessing.FeatureSelection.Filter

Selects the K best features based on a scoring function (scikit-learn’s SelectKBest).

Inheritance: TransformerBase<T, Matrix<T>, Matrix<T>> → SelectKBest<T>

Implements: ISupervisedFeatureSelector<T>

For Beginners

SelectKBest is the simplest approach - compute a score for each feature independently and keep the top K. It’s fast and works well when features are truly independent, but may miss interactions between features.

How It Works

A simple univariate feature selection that computes a score for each feature and selects the K highest scoring features. As in scikit-learn the default scoring function is the ANOVA F-value for classification (FClassif, f_classif). For a continuous (regression) target pass FRegression (f_regression) or MutualInfoRegression, or supply any scorer of your own; the target is never re-interpreted behind your back.

Constructors

Constructor Summary
SelectKBest(int k, Func<Matrix<T>, Vector<T>, ValueTuple<Vector<T>, Vector<T>>> scoreFunction) Creates a selector with a custom scorer, like scikit-learn’s score_func: it receives the data and the target and returns one score per feature (higher is better) and optionally one p-value per feature.
SelectKBest(int k, SelectKBestScoreFunc scoreFunction, int[] columnIndices) Creates a selector that scores features with a built-in scikit-learn scoring function.

Properties

Property Summary
SelectKBestScoreFunc ScoreFunction { get; } Gets the built-in scoring function, used when no custom scorer was supplied.
Vector<T> PValues { get; } Gets the p-value of every feature after fitting, or null when the scoring function has none.
Vector<T> Scores { get; } Gets the score of every feature after fitting.
bool SupportsInverseTransform { get; }
int K { get; } Gets the number of features to select.
int[] SelectedIndices { get; }

Methods

Method Summary
Matrix<T> FitTransform(Matrix<T> data, Vector<T> target)
bool[] GetSupportMask()
string[] GetFeatureNamesOut(string[] inputFeatureNames)
void Fit(Matrix<T> data, Vector<T> target)