Table of Contents

Class SequentialFeatureSelector<T>

Namespace
AiDotNet.Preprocessing.FeatureSelection
Assembly
AiDotNet.dll

Sequential feature selection using forward or backward selection.

public class SequentialFeatureSelector<T> : TransformerBase<T, Matrix<T>, Matrix<T>>, IDataTransformer<T, Matrix<T>, Matrix<T>>

Type Parameters

T

The numeric type for calculations (e.g., float, double).

Inheritance
TransformerBase<T, Matrix<T>, Matrix<T>>
SequentialFeatureSelector<T>
Implements
IDataTransformer<T, Matrix<T>, Matrix<T>>
Inherited Members

Remarks

SequentialFeatureSelector performs feature selection by sequentially adding or removing features based on cross-validation scores.

- Forward selection: Start with no features, add the best one at each step - Backward selection: Start with all features, remove the worst one at each step

For Beginners: This is like building a team: - Forward: Start with no players, add the best available each round - Backward: Start with everyone, remove the weakest each round - Stop when you have the desired team size

Constructors

SequentialFeatureSelector(int, SequentialDirection, int, Func<double[], double[], double>?, int[]?)

Creates a new instance of SequentialFeatureSelector<T>.

public SequentialFeatureSelector(int nFeaturesToSelect, SequentialDirection direction = SequentialDirection.Forward, int cv = 5, Func<double[], double[], double>? scoringFunc = null, int[]? columnIndices = null)

Parameters

nFeaturesToSelect int

Number of features to select.

direction SequentialDirection

Selection direction (Forward or Backward). Defaults to Forward.

cv int

Number of cross-validation folds. Defaults to 5.

scoringFunc Func<double[], double[], double>

Custom scoring function (y_true, y_pred) => score. Null for R² score.

columnIndices int[]

The column indices to evaluate, or null for all columns.

Properties

Direction

Gets the selection direction.

public SequentialDirection Direction { get; }

Property Value

SequentialDirection

NFeaturesToSelect

Gets the number of features to select.

public int NFeaturesToSelect { get; }

Property Value

int

SelectedIndices

Gets the indices of selected features.

public int[]? SelectedIndices { get; }

Property Value

int[]

SupportsInverseTransform

Gets whether this transformer supports inverse transformation.

public override bool SupportsInverseTransform { get; }

Property Value

bool

Methods

Fit(Matrix<T>, Vector<T>)

Fits the selector using sequential selection with cross-validation.

public void Fit(Matrix<T> data, Vector<T> target)

Parameters

data Matrix<T>

The feature matrix.

target Vector<T>

The target values.

FitCore(Matrix<T>)

Fits the selector (requires target via specialized Fit method).

protected override void FitCore(Matrix<T> data)

Parameters

data Matrix<T>

FitTransform(Matrix<T>, Vector<T>)

Fits and transforms the data.

public Matrix<T> FitTransform(Matrix<T> data, Vector<T> target)

Parameters

data Matrix<T>
target Vector<T>

Returns

Matrix<T>

GetFeatureNamesOut(string[]?)

Gets the output feature names after transformation.

public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)

Parameters

inputFeatureNames string[]

Returns

string[]

GetSupportMask()

Gets the support mask indicating which features are selected.

public bool[] GetSupportMask()

Returns

bool[]

InverseTransformCore(Matrix<T>)

Inverse transformation is not supported.

protected override Matrix<T> InverseTransformCore(Matrix<T> data)

Parameters

data Matrix<T>

Returns

Matrix<T>

TransformCore(Matrix<T>)

Transforms the data by selecting the chosen features.

protected override Matrix<T> TransformCore(Matrix<T> data)

Parameters

data Matrix<T>

Returns

Matrix<T>