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
TThe numeric type for calculations (e.g., float, double).
- Inheritance
-
SequentialFeatureSelector<T>
- Implements
- 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
nFeaturesToSelectintNumber of features to select.
directionSequentialDirectionSelection direction (Forward or Backward). Defaults to Forward.
cvintNumber of cross-validation folds. Defaults to 5.
scoringFuncFunc<double[], double[], double>Custom scoring function (y_true, y_pred) => score. Null for R² score.
columnIndicesint[]The column indices to evaluate, or null for all columns.
Properties
Direction
Gets the selection direction.
public SequentialDirection Direction { get; }
Property Value
NFeaturesToSelect
Gets the number of features to select.
public int NFeaturesToSelect { get; }
Property Value
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
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
dataMatrix<T>The feature matrix.
targetVector<T>The target values.
FitCore(Matrix<T>)
Fits the selector (requires target via specialized Fit method).
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>
FitTransform(Matrix<T>, Vector<T>)
Fits and transforms the data.
public Matrix<T> FitTransform(Matrix<T> data, Vector<T> target)
Parameters
dataMatrix<T>targetVector<T>
Returns
- Matrix<T>
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]
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
dataMatrix<T>
Returns
- Matrix<T>
TransformCore(Matrix<T>)
Transforms the data by selecting the chosen features.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>
Returns
- Matrix<T>