Class VarianceThreshold<T>
- Namespace
- AiDotNet.Preprocessing.FeatureSelection
- Assembly
- AiDotNet.dll
Feature selector that removes features with variance below a threshold.
public class VarianceThreshold<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
-
VarianceThreshold<T>
- Implements
- Inherited Members
Remarks
VarianceThreshold removes all features whose variance doesn't meet a minimum threshold. Features with low variance are often not informative because they don't vary enough across samples to be useful for prediction.
For Beginners: If a feature has the same value (or nearly the same value) for all samples, it won't help your model distinguish between different outcomes. This transformer automatically removes such features: - Constant features (all same value) have variance = 0 - Near-constant features have very low variance
Example: A "Country" column that is "USA" for 99.9% of rows provides little information.
Constructors
VarianceThreshold(double, int[]?)
Creates a new instance of VarianceThreshold<T>.
public VarianceThreshold(double threshold = 0, int[]? columnIndices = null)
Parameters
thresholddoubleFeatures with variance below this are removed. Defaults to 0.0.
columnIndicesint[]The column indices to consider, or null for all columns.
Properties
SelectedFeatures
Gets the indices of selected features.
public int[]? SelectedFeatures { get; }
Property Value
- int[]
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
Threshold
Gets the variance threshold.
public double Threshold { get; }
Property Value
Variances
Gets the computed variances for each feature.
public double[]? Variances { get; }
Property Value
- double[]
Methods
FitCore(Matrix<T>)
Computes the variance of each feature.
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>The training data matrix.
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]
Returns
- string[]
GetSupportMask()
Gets a boolean mask indicating which features are selected.
public bool[] GetSupportMask()
Returns
- bool[]
Array where true indicates the feature is selected.
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>)
Removes features with variance below the threshold.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The data with low-variance features removed.