LeaveOneOutEncoder<T>

Models & Types · AiDotNet.Preprocessing.Encoders

Encodes categorical features using leave-one-out target encoding.

Inheritance: TransformerBase<T, Matrix<T>, Matrix<T>> → LeaveOneOutEncoder<T>

For Beginners

Regular target encoding can overfit because it uses the same data to encode and train. Leave-one-out encoding prevents this:

  • When encoding row 1, it uses the average of all OTHER rows with the same category
  • This prevents the model from “cheating” by memorizing individual samples

Example: If “Category A” has 3 samples with targets [1, 0, 1]:

  • Row 1 gets encoded as average of [0, 1] = 0.5
  • Row 2 gets encoded as average of [1, 1] = 1.0
  • Row 3 gets encoded as average of [1, 0] = 0.5

How It Works

LeaveOneOutEncoder is similar to TargetEncoder but uses leave-one-out statistics to prevent overfitting. For each sample, the encoding is computed using all other samples in the same category, excluding the current sample.

This reduces the risk of target leakage during training while still capturing the relationship between categories and the target variable.

Constructors

Constructor Summary
LeaveOneOutEncoder(double smoothing, LeaveOneOutHandleUnknown handleUnknown, int[] columnIndices) Creates a new instance of LeaveOneOutEncoder.

Properties

Property Summary
LeaveOneOutHandleUnknown HandleUnknown { get; } Gets how unknown categories are handled.
T GlobalMean { get; } Gets the global target mean.
bool SupportsInverseTransform { get; } Gets whether this transformer supports inverse transformation.
double Smoothing { get; } Gets the smoothing parameter.

Methods

Method Summary
Matrix<T> FitTransform(Matrix<T> data, Vector<T> target) Fits and transforms using leave-one-out encoding.
Matrix<T> TransformWithTarget(Matrix<T> data, Vector<T> target) Transforms the data using leave-one-out encoding (requires target for training data).
string[] GetFeatureNamesOut(string[] inputFeatureNames) Gets the output feature names after transformation.
void Fit(Matrix<T> data, Vector<T> target) Fits the encoder by computing category statistics.