Class FunctionTransformer<T>
- Namespace
- AiDotNet.Preprocessing.FeatureGeneration
- Assembly
- AiDotNet.dll
Applies a custom function to transform data.
public class FunctionTransformer<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
-
FunctionTransformer<T>
- Implements
- Inherited Members
Remarks
FunctionTransformer allows you to apply arbitrary functions to your data as part of a preprocessing pipeline. This is useful for applying domain-specific transformations or wrapping legacy code into the transformer API.
For Beginners: Sometimes you need a custom transformation that doesn't fit standard transformers. FunctionTransformer lets you plug in your own function: - Apply a mathematical formula to all values - Perform domain-specific feature engineering - Wrap existing transformation code
Example: Apply a custom normalization formula or domain-specific scaling.
Constructors
FunctionTransformer(Func<Matrix<T>, Matrix<T>>?, Func<Matrix<T>, Matrix<T>>?, bool, int[]?)
Creates a new instance of FunctionTransformer<T> with matrix-level functions.
public FunctionTransformer(Func<Matrix<T>, Matrix<T>>? func = null, Func<Matrix<T>, Matrix<T>>? inverseFunc = null, bool validate = true, int[]? columnIndices = null)
Parameters
funcFunc<Matrix<T>, Matrix<T>>The function to apply to the entire matrix. If null, acts as identity.
inverseFuncFunc<Matrix<T>, Matrix<T>>The inverse function (optional).
validateboolIf true, validates that output has same number of rows as input.
columnIndicesint[]The column indices to transform, or null for all columns.
Properties
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
Validate
Gets whether this transformer validates input/output shapes.
public bool Validate { get; }
Property Value
Methods
Abs()
Creates a FunctionTransformer that applies the absolute value.
public static FunctionTransformer<T> Abs()
Returns
Clip(double, double)
Creates a FunctionTransformer that clips values to a range.
public static FunctionTransformer<T> Clip(double min, double max)
Parameters
Returns
Exp()
Creates a FunctionTransformer that applies the exponential function.
public static FunctionTransformer<T> Exp()
Returns
FitCore(Matrix<T>)
Stores the input dimensions for validation.
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>The training data matrix.
FromElementFunction(Func<double, double>, Func<double, double>?, bool, int[]?)
Creates a new instance of FunctionTransformer<T> with element-wise functions.
public static FunctionTransformer<T> FromElementFunction(Func<double, double> elementFunc, Func<double, double>? inverseElementFunc = null, bool validate = true, int[]? columnIndices = null)
Parameters
elementFuncFunc<double, double>The function to apply to each element.
inverseElementFuncFunc<double, double>The inverse function for each element (optional).
validateboolIf true, validates shapes.
columnIndicesint[]The column indices to transform, or null for all columns.
Returns
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]
Returns
- string[]
InverseTransformCore(Matrix<T>)
Applies the inverse transformation function to the data.
protected override Matrix<T> InverseTransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The transformed data.
Returns
- Matrix<T>
The original-scale data.
Log()
Creates a FunctionTransformer that applies the natural logarithm.
public static FunctionTransformer<T> Log()
Returns
Log1p()
Creates a FunctionTransformer that applies log(1 + x).
public static FunctionTransformer<T> Log1p()
Returns
Power(double)
Creates a FunctionTransformer that raises to a power.
public static FunctionTransformer<T> Power(double power)
Parameters
powerdoubleThe power to raise values to.
Returns
Sign()
Creates a FunctionTransformer that applies the sign function.
public static FunctionTransformer<T> Sign()
Returns
Sqrt()
Creates a FunctionTransformer that applies the square root.
public static FunctionTransformer<T> Sqrt()
Returns
TransformCore(Matrix<T>)
Applies the transformation function to the data.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The transformed data.