Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Category Encoders: Catalog & Experiments (Part 2)

We get 24 encoders from 4 libraries:

libraryone-hot encodersother simple encoderscontrast encoderstarget/Bayesian encoders
sklearn.preprocessingOneHotEncoderLabelEncoder
OrdinalEncoder
LabelBinarizer
category_encodersOneHotEncoderOrdinalEncoder
BinaryEncoder
BaseNEncoder
CountEncoder
HashingEncoder
HelmertEncoder
SumEncoder
BackwardDifferenceEncoder
PolynomialEncoder
TargetEncoder
LeaveOneOutEncoder
CatBoostEncoder

MEstimateEncoder
WOEEncoder
JamesSteinEncoder
GLMMEncoder
pandasget_dummiesfactorize
keras.utilsto_categorical


This notebook explores step-by-step the Hashing Encoder, the Polynomial Encoder and some flavours/variations of the target encoder. All flavours of target encoders peeks into the target; we therefore need to be mindful of data leakage. Options are available to regulate/control data leakage and overfitting. TargetEncoder is the vanilla flavour. LeaveOneOutEncoder is the conservative option, where a given sample sees other samples’ target but blindfolded from it’s own. CatBoostEncoder is sensitive to row ordering; a given sample sees the target of preceding samples only. JamesSteinEncoder is for normal distributions.

In particular, we have in this notebook

  • TargetEncoder (the vanilla form) demonstrated in detail to tell the principle behind target encoding, which underlies all flavours of target encoding. Manual back-of-envelop derivation is compared with automated output from TargetEncoder.

  • LeaveOneOutEncoder demonstrated in detail as a conservative step up to reduce data leakage and overfitting. Manual back-of-envelop derivation is compared with automated output from LeaveOneOutEncoder.


This notebook continues from an earlier notebook, Category Encoders: Catalog & Experiments (Part 1).


When to use which encoder to solve what problems? There is a good guide here: [Encode Smarter: How to Easily Integrate Categorical Encoding into Your Machine Learning Pipeline](https://innovation.alteryx.com/encode-smarter).

Loading...
(299, array(['LO', 'HJ', 'DJ', 'KV', 'DP', 'GE', 'HQ', 'HC', 'EK', 'GS', 'HG', 'BY', 'HX', 'JK', 'FJ', 'LM', 'HK', 'MD', 'IG', 'JG', 'AN', 'AD', 'MC', 'KW', 'CK', 'LF', 'CS', 'GK', 'DC', 'LB', 'FM', 'IH', 'LN', 'IK', 'DF', 'IB', 'CB', 'LY', 'JW', 'FI', 'CR', 'IE', 'LE', 'HB', 'HV', 'LG', 'BG', 'KP', 'LI', 'HL', 'BF', 'LU', 'O', 'GI', 'DQ', 'IR', 'DV', 'HA', 'KB', 'FP', 'AT', 'IF', 'HN', 'GC', 'C', 'KC', 'G', 'JA', 'CU', 'BC', 'AB', 'KF', 'MB', 'HE', 'BL', 'FQ', 'IA', 'MJ', 'FO', 'V', 'JT', 'AU', 'IO', 'GQ', 'CC', 'JR', 'BM', 'HH', 'AV', 'GT', 'I', 'IU', 'JN', 'EV', 'MV', 'EQ', 'LW', 'FN', 'IT', 'AA', 'DK', 'IJ', 'GU', 'P', 'JH', 'CM', 'GA', 'R', 'LX', 'IX', 'DY', 'D', 'FL', 'CP', 'GL', 'DI', 'CD', 'IV', 'FS', 'FR', 'J', 'MP', 'MH', 'EL', 'JD', 'AP', 'AE', 'F', 'LC', 'BP', 'BI', 'MF', 'DO', 'MG', 'MT', 'LD', 'CW', 'KS', 'BV', 'JV', 'BB', 'AM', 'KX', 'FK', 'AH', 'LV', 'W', 'DU', 'FB', 'JX', 'KA', 'CO', 'AR', 'KR', 'JI', 'T', 'JP', 'LQ', 'FX', 'FD', 'EY', 'Y', 'JO', 'EC', 'HM', 'AC', 'DW', 'HU', 'FH', 'AY', 'AL', 'GD', 'GB', 'DS', 'FT', 'KH', 'CG', 'JB', 'E', 'CN', 'BT', 'X', 'BX', 'HW', 'EI', 'ID', 'KT', 'GR', 'L', 'KG', 'EA', 'HO', 'GX', 'K', 'AS', 'DM', 'AK', 'FC', 'MS', 'HR', 'EU', 'ES', 'JY', 'HP', 'KL', 'FE', 'CY', 'EO', 'KJ', 'CJ', 'CI', 'JL', 'IC', 'S', 'DH', 'GN', 'BS', 'AG', 'M', 'EW', 'FA', 'LJ', 'GJ', 'KQ', 'HF', 'MR', 'BQ', 'ED', 'FG', 'LL', 'EG', 'HY', 'EH', 'GW', 'BD', 'IQ', 'Q', 'DA', 'DD', 'GM', 'KN', 'MQ', 'GY', 'KD', 'JJ', 'CL', 'IY', 'KU', 'CT', 'KK', 'DN', 'BO', 'IP', 'LH', 'IM', 'DE', 'ME', 'EE', 'LT', 'LR', 'MI', 'CF', 'DR', 'EB', 'KI', 'DX', 'DL', 'MW', 'FF', 'EF', 'EP', 'MU', 'MA', 'GG', 'CQ', 'DT', 'FV', 'CH', 'AF', 'AJ', 'IN', 'JC', 'EN', 'JU', 'JE', 'ML', 'AW', 'HI', 'MO', 'GF', 'MK', 'GH', 'FW', 'GV', 'JF', 'BA', 'LK', 'IL', 'CX'], dtype=object))
(84, array(['BI', 'AB', 'BU', 'M', 'T', 'K', 'L', 'CG', 'BG', 'CI', 'N', 'G', 'X', 'Q', 'O', 'BO', 'BB', 'BX', 'AF', 'BA', 'BQ', 'CA', 'D', 'AQ', 'AS', 'AW', 'BE', 'CK', 'AL', 'BK', 'AT', 'CL', 'C', 'CF', 'I', 'AH', 'CD', 'AY', 'BY', 'F', 'AI', 'R', 'BC', 'BH', 'AA', 'V', 'CE', 'BD', 'AE', 'U', 'AU', 'AP', 'CJ', 'AN', 'AX', 'AR', 'BL', 'J', 'ZZ', 'BR', 'BV', 'H', 'A', 'CC', 'P', 'CH', 'BJ', 'CB', 'BS', 'BN', 'AO', 'AJ', 'BT', 'S', 'E', 'Y', 'AK', 'AM', 'B', 'BM', 'AV', 'AG', 'BF', 'BP'], dtype=object))

1. Hashing Encoder

What is a Hashing Encoder? The question becomes immediately self-explanatory the moment we read the word hashing in the light of MD5, SHA, .... Yes, it’s that same hash that the hashing encoder is about.

HashingEncoder takes n_components as an argument. Let us do a test with n_components= 8, 16, 32:

CPU times: user 932 ms, sys: 1.2 s, total: 2.13 s
Wall time: 6min 56s
Loading...
Loading...
Loading...
Loading...
n_components   unique values of output
8              8
16             16
32             32

2. Polynomial encoder

Loading...
Loading...
Loading...

3. Target Encoders

GLMMEncoder is order-dependent.
CatBoostEncoder is order-dependent.
Loading...

3.1 Target Encoder (vanilla)

<Figure size 1080x1080 with 1 Axes>
True

3.2 Leave-One-Out Encoder

LeaveOneOutEncoder is the conservative step up from the vanilla TargetEncoder. It reduces data leakage and overfitting by taking the target mean from rows other than a given row. A worked back-of-envelop example would explain best:

Loading...
<Figure size 432x288 with 1 Axes>
False
108 BF 1083 BX 2223 CC 2329 AM 2956 BM 3827 R 4114 B 5235 J 6985 CG 8167 CB 8372 BJ 9533 BH 9687 P Name: cat5, dtype: object
BF 1 BX 1 CC 1 AM 1 BM 1 R 1 B 1 J 1 CG 1 CB 1 BJ 1 BH 1 P 1 Name: cat5, dtype: int64
Loading...
0.2626