Table of Contents

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

T

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

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

func Func<Matrix<T>, Matrix<T>>

The function to apply to the entire matrix. If null, acts as identity.

inverseFunc Func<Matrix<T>, Matrix<T>>

The inverse function (optional).

validate bool

If true, validates that output has same number of rows as input.

columnIndices int[]

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

bool

Validate

Gets whether this transformer validates input/output shapes.

public bool Validate { get; }

Property Value

bool

Methods

Abs()

Creates a FunctionTransformer that applies the absolute value.

public static FunctionTransformer<T> Abs()

Returns

FunctionTransformer<T>

Clip(double, double)

Creates a FunctionTransformer that clips values to a range.

public static FunctionTransformer<T> Clip(double min, double max)

Parameters

min double

Minimum value.

max double

Maximum value.

Returns

FunctionTransformer<T>

Exp()

Creates a FunctionTransformer that applies the exponential function.

public static FunctionTransformer<T> Exp()

Returns

FunctionTransformer<T>

FitCore(Matrix<T>)

Stores the input dimensions for validation.

protected override void FitCore(Matrix<T> data)

Parameters

data Matrix<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

elementFunc Func<double, double>

The function to apply to each element.

inverseElementFunc Func<double, double>

The inverse function for each element (optional).

validate bool

If true, validates shapes.

columnIndices int[]

The column indices to transform, or null for all columns.

Returns

FunctionTransformer<T>

GetFeatureNamesOut(string[]?)

Gets the output feature names after transformation.

public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)

Parameters

inputFeatureNames string[]

Returns

string[]

InverseTransformCore(Matrix<T>)

Applies the inverse transformation function to the data.

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

Parameters

data Matrix<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

FunctionTransformer<T>

Log1p()

Creates a FunctionTransformer that applies log(1 + x).

public static FunctionTransformer<T> Log1p()

Returns

FunctionTransformer<T>

Power(double)

Creates a FunctionTransformer that raises to a power.

public static FunctionTransformer<T> Power(double power)

Parameters

power double

The power to raise values to.

Returns

FunctionTransformer<T>

Sign()

Creates a FunctionTransformer that applies the sign function.

public static FunctionTransformer<T> Sign()

Returns

FunctionTransformer<T>

Sqrt()

Creates a FunctionTransformer that applies the square root.

public static FunctionTransformer<T> Sqrt()

Returns

FunctionTransformer<T>

TransformCore(Matrix<T>)

Applies the transformation function to the data.

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

Parameters

data Matrix<T>

The data to transform.

Returns

Matrix<T>

The transformed data.