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
TThe numeric type for calculations (e.g., float, double).
- Inheritance
-
RFECV<T>
- Implements
- 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
minFeaturesToSelectintMinimum number of features to consider. Defaults to 1.
stepintNumber of features to remove at each iteration. Defaults to 1.
cvintNumber of cross-validation folds. Defaults to 5.
importanceMethodRFEImportanceMethodMethod for computing feature importance. Defaults to Correlation.
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
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
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
Methods
Fit(Matrix<T>, Vector<T>)
Fits RFECV using 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 optimal features.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>
Returns
- Matrix<T>