Class SelectFromModel<T>
- Namespace
- AiDotNet.Preprocessing.FeatureSelection
- Assembly
- AiDotNet.dll
Selects features based on importance weights from an external model or scorer.
public class SelectFromModel<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
-
SelectFromModel<T>
- Implements
- Inherited Members
Remarks
SelectFromModel selects features based on importance scores, typically from a fitted model. Features with importance above a threshold are kept.
The threshold can be specified as: - An absolute value - "mean" - the mean of feature importances - "median" - the median of feature importances
For Beginners: This works with any model that produces feature importances: - Random forests give importance based on how much each feature reduces error - Linear models give coefficients showing feature influence - Features below the threshold are removed
Constructors
SelectFromModel(double[], SelectFromModelThreshold, double, int?, int[]?)
Creates a new instance with precomputed feature importances.
public SelectFromModel(double[] featureImportances, SelectFromModelThreshold thresholdType = SelectFromModelThreshold.Mean, double thresholdValue = 0, int? maxFeatures = null, int[]? columnIndices = null)
Parameters
featureImportancesdouble[]Precomputed feature importance scores.
thresholdTypeSelectFromModelThresholdType of threshold to use. Defaults to Mean.
thresholdValuedoubleThreshold value (used when type is Value). Defaults to 0.
maxFeaturesint?Maximum number of features to select. Null for no limit.
columnIndicesint[]The column indices to evaluate, or null for all columns.
SelectFromModel(Func<Matrix<T>, Vector<T>, double[]>, SelectFromModelThreshold, double, int?, int[]?)
Creates a new instance with a function to compute feature importances.
public SelectFromModel(Func<Matrix<T>, Vector<T>, double[]> importanceFunc, SelectFromModelThreshold thresholdType = SelectFromModelThreshold.Mean, double thresholdValue = 0, int? maxFeatures = null, int[]? columnIndices = null)
Parameters
importanceFuncFunc<Matrix<T>, Vector<T>, double[]>Function that computes feature importances given data and target.
thresholdTypeSelectFromModelThresholdType of threshold to use. Defaults to Mean.
thresholdValuedoubleThreshold value (used when type is Value). Defaults to 0.
maxFeaturesint?Maximum number of features to select. Null for no limit.
columnIndicesint[]The column indices to evaluate, or null for all columns.
Properties
FeatureImportances
Gets the feature importances used for selection.
public double[]? FeatureImportances { get; }
Property Value
- double[]
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
Threshold
Gets the computed threshold value.
public double Threshold { get; }
Property Value
Methods
Fit(Matrix<T>, Vector<T>)
Fits the selector by computing feature importances.
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 using precomputed importances only.
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 important features.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>
Returns
- Matrix<T>