Class RFE<T>
- Namespace
- AiDotNet.Preprocessing.FeatureSelection
- Assembly
- AiDotNet.dll
Recursive Feature Elimination for feature selection.
public class RFE<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
-
RFE<T>
- Implements
- Inherited Members
Remarks
RFE performs feature selection by recursively removing features and building a model on the remaining features. It ranks features by importance and removes the least important ones until the desired number of features is reached.
The algorithm: 1. Train a model on all features and compute feature importances 2. Remove the least important feature(s) 3. Repeat until desired number of features is reached
For Beginners: RFE is like an elimination tournament for features: - Start with all features - Remove the weakest performer each round - Keep going until you have the desired number of features - The surviving features are the most important ones
Constructors
RFE(int, int, RFEImportanceMethod, int[]?)
Creates a new instance of RFE<T>.
public RFE(int nFeaturesToSelect = 5, int step = 1, RFEImportanceMethod importanceMethod = RFEImportanceMethod.Correlation, int[]? columnIndices = null)
Parameters
nFeaturesToSelectintNumber of features to select. Defaults to 5.
stepintNumber of features to remove at each iteration. Defaults to 1.
importanceMethodRFEImportanceMethodMethod for computing feature importance. Defaults to Correlation.
columnIndicesint[]The column indices to evaluate, or null for all columns.
Properties
FeatureImportances
Gets the feature importances from the final model.
public double[]? FeatureImportances { get; }
Property Value
- double[]
NFeaturesToSelect
Gets the number of features to select.
public int NFeaturesToSelect { get; }
Property Value
Ranking
Gets the feature ranking (1 = selected, 2+ = elimination order).
public int[]? Ranking { get; }
Property Value
- int[]
SelectedIndices
Gets the indices of selected features.
public int[]? SelectedIndices { get; }
Property Value
- int[]
Step
Gets the step size (features removed per iteration).
public int Step { get; }
Property Value
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
Methods
Fit(Matrix<T>, Vector<T>)
Fits RFE by recursively eliminating features.
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 most important features.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>
Returns
- Matrix<T>