Class TransformerBase<T, TInput, TOutput>
- Namespace
- AiDotNet.Preprocessing
- Assembly
- AiDotNet.dll
Abstract base class for all data transformers providing common functionality.
public abstract class TransformerBase<T, TInput, TOutput> : IDataTransformer<T, TInput, TOutput>
Type Parameters
TThe numeric type for calculations (e.g., float, double).
TInputThe input data type.
TOutputThe output data type after transformation.
- Inheritance
-
TransformerBase<T, TInput, TOutput>
- Implements
-
IDataTransformer<T, TInput, TOutput>
- Derived
- Inherited Members
Remarks
This class provides the template method pattern for data transformation. Derived classes implement the core fitting and transformation logic while this base class handles validation, state management, and common operations.
For Beginners: This is the foundation that all transformers build on. It provides common features like: - Checking if the transformer is ready to use - Managing which columns to transform - Serialization for saving/loading transformers
When creating a new transformer, you extend this class and implement the abstract methods.
Constructors
TransformerBase(int[]?)
Creates a new instance of the transformer.
protected TransformerBase(int[]? columnIndices = null)
Parameters
columnIndicesint[]The column indices to operate on, or null for all columns.
Properties
ColumnIndices
Gets the column indices this transformer operates on.
public int[]? ColumnIndices { get; }
Property Value
- int[]
Engine
Gets the computational engine for tensor operations.
protected IEngine Engine { get; }
Property Value
- IEngine
IsFitted
Gets whether this transformer has been fitted to data.
public bool IsFitted { get; protected set; }
Property Value
NumOps
Gets the numeric operations helper for type T.
protected INumericOperations<T> NumOps { get; }
Property Value
- INumericOperations<T>
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public abstract bool SupportsInverseTransform { get; }
Property Value
Methods
EnsureFitted()
Ensures the transformer has been fitted before transformation.
protected void EnsureFitted()
Exceptions
- InvalidOperationException
Thrown if not fitted.
Fit(TInput)
Fits the transformer to the training data.
public void Fit(TInput data)
Parameters
dataTInputThe training data to fit.
FitCore(TInput)
Core fitting implementation. Override this in derived classes.
protected abstract void FitCore(TInput data)
Parameters
dataTInputThe training data to fit.
FitTransform(TInput)
Fits the transformer and transforms the data in a single step.
public TOutput FitTransform(TInput data)
Parameters
dataTInputThe data to fit and transform.
Returns
- TOutput
The transformed data.
GetColumnsToProcess(int)
Gets the indices to operate on, defaulting to all if ColumnIndices is null.
protected int[] GetColumnsToProcess(int totalColumns)
Parameters
totalColumnsintThe total number of columns in the data.
Returns
- int[]
The column indices to process.
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public virtual string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]The input feature names (optional).
Returns
- string[]
The output feature names.
InverseTransform(TOutput)
Reverses the transformation.
public TInput InverseTransform(TOutput data)
Parameters
dataTOutputThe transformed data.
Returns
- TInput
The original-scale data.
Exceptions
- NotSupportedException
Thrown if inverse transform is not supported.
- InvalidOperationException
Thrown if Fit() has not been called.
InverseTransformCore(TOutput)
Core inverse transformation implementation. Override this in derived classes.
protected virtual TInput InverseTransformCore(TOutput data)
Parameters
dataTOutputThe transformed data.
Returns
- TInput
The original-scale data.
Transform(TInput)
Transforms the input data using fitted parameters.
public TOutput Transform(TInput data)
Parameters
dataTInputThe data to transform.
Returns
- TOutput
The transformed data.
Exceptions
- InvalidOperationException
Thrown if Fit() has not been called.
TransformCore(TInput)
Core transformation implementation. Override this in derived classes.
protected abstract TOutput TransformCore(TInput data)
Parameters
dataTInputThe data to transform.
Returns
- TOutput
The transformed data.
ValidateInputData(TInput)
Validates input data before fitting or transforming.
protected virtual void ValidateInputData(TInput data)
Parameters
dataTInputThe data to validate.
Exceptions
- ArgumentNullException
Thrown if data is null.