Table of Contents

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

T

The numeric type for calculations (e.g., float, double).

Inheritance
TransformerBase<T, Matrix<T>, Matrix<T>>
SelectFromModel<T>
Implements
IDataTransformer<T, Matrix<T>, Matrix<T>>
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

featureImportances double[]

Precomputed feature importance scores.

thresholdType SelectFromModelThreshold

Type of threshold to use. Defaults to Mean.

thresholdValue double

Threshold value (used when type is Value). Defaults to 0.

maxFeatures int?

Maximum number of features to select. Null for no limit.

columnIndices int[]

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

importanceFunc Func<Matrix<T>, Vector<T>, double[]>

Function that computes feature importances given data and target.

thresholdType SelectFromModelThreshold

Type of threshold to use. Defaults to Mean.

thresholdValue double

Threshold value (used when type is Value). Defaults to 0.

maxFeatures int?

Maximum number of features to select. Null for no limit.

columnIndices int[]

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

bool

Threshold

Gets the computed threshold value.

public double Threshold { get; }

Property Value

double

Methods

Fit(Matrix<T>, Vector<T>)

Fits the selector by computing feature importances.

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 using precomputed importances only.

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 important features.

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

Parameters

data Matrix<T>

Returns

Matrix<T>