The TargetEncoder uses the value of the target to encode each
categorical feature. In this example, we will compare three different approaches
for handling categorical features: TargetEncoder,
OrdinalEncoder, OneHotEncoder and dropping the category.
# Authors: The scikit-learn developers# SPDX-License-Identifier: BSD-3-Clause
Loading Data from OpenML
First, we load the wine reviews dataset, where the target is the points given
be a reviewer:
Training and Evaluating Pipelines with Different Encoders
In this section, we will evaluate pipelines with
HistGradientBoostingRegressor with different encoding
strategies. First, we list out the encoders we will be using to preprocess
the categorical features:
In this section, we build and evaluate a pipeline that uses native categorical
feature support in HistGradientBoostingRegressor,
which only supports up to 255 unique categories. In our dataset, the most of
the categorical features have more than 255 unique categories:
winery 14810
region_1 1236
variety 632
province 455
country 48
region_2 18
dtype: int64
To workaround the limitation above, we group the categorical features into
low cardinality and high cardinality features. The high cardinality features
will be target encoded and the low cardinality features will use the native
categorical feature in gradient boosting.
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Finally, we evaluate the pipeline using cross validation and record the results:
When evaluating the predictive performance on the test set, dropping the
categories perform the worst and the target encoders performs the best. This
can be explained as follows:
Dropping the categorical features makes the pipeline less expressive and
underfitting as a result;
Due to the high cardinality and to reduce the training time, the one-hot
encoding scheme uses max_categories=20 which prevents the features from
expanding too much, which can result in underfitting.
If we had not set max_categories=20, the one-hot encoding scheme would have
likely made the pipeline overfitting as the number of features explodes with rare
category occurrences that are correlated with the target by chance (on the training
set only);
The ordinal encoding imposes an arbitrary order to the features which are then
treated as numerical values by the
HistGradientBoostingRegressor. Since this
model groups numerical features in 256 bins per feature, many unrelated categories
can be grouped together and as a result overall pipeline can underfit;
When using the target encoder, the same binning happens, but since the encoded
values are statistically ordered by marginal association with the target variable,
the binning use by the HistGradientBoostingRegressor
makes sense and leads to good results: the combination of smoothed target
encoding and binning works as a good regularizing strategy against
overfitting while not limiting the expressiveness of the pipeline too much.
Total running time of the script: (0 minutes 20.973 seconds)