Table of Contents

Class RFECV<T>

Namespace
AiDotNet.Preprocessing.FeatureSelection
Assembly
AiDotNet.dll

Recursive Feature Elimination with Cross-Validation to find optimal feature count.

public class RFECV<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>>
RFECV<T>
Implements
IDataTransformer<T, Matrix<T>, Matrix<T>>
Inherited Members

Remarks

RFECV performs RFE multiple times using cross-validation to determine the optimal number of features. It evaluates model performance at each step of elimination.

The algorithm: 1. For each fold, run RFE and record validation scores at each number of features 2. Average scores across folds 3. Select the number of features with best average score

For Beginners: RFECV automatically finds how many features to keep: - Regular RFE requires you to specify the number of features - RFECV tests different numbers and picks the best one - Uses cross-validation to avoid overfitting the feature selection

Constructors

RFECV(int, int, int, RFEImportanceMethod, Func<double[], double[], double>?, int[]?)

Creates a new instance of RFECV<T>.

public RFECV(int minFeaturesToSelect = 1, int step = 1, int cv = 5, RFEImportanceMethod importanceMethod = RFEImportanceMethod.Correlation, Func<double[], double[], double>? scoringFunc = null, int[]? columnIndices = null)

Parameters

minFeaturesToSelect int

Minimum number of features to consider. Defaults to 1.

step int

Number of features to remove at each iteration. Defaults to 1.

cv int

Number of cross-validation folds. Defaults to 5.

importanceMethod RFEImportanceMethod

Method for computing feature importance. Defaults to Correlation.

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

CVScores

Gets the cross-validation scores at each number of features.

public double[]? CVScores { get; }

Property Value

double[]

NFeatures

Gets the number of features selected.

public int NFeatures { get; }

Property Value

int

Ranking

Gets the feature ranking.

public int[]? Ranking { 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 RFECV using 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 optimal features.

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

Parameters

data Matrix<T>

Returns

Matrix<T>