Before you start
Encode a categorical feature as the smoothed mean of the target for each category.
One-hot encoding a column with thousands of categories makes thousands of
columns. Target encoding makes one: each category is replaced by the average
target of the training rows in that category. Rare categories have noisy means,
so blend each mean with the global mean. The blend uses m, the number of
"virtual" rows at the global mean:
enc(c) = (n_c * mean_c + m * global_mean) / (n_c + m)
n_c is the count of category c in the training data, mean_c is the mean
target over those rows, and global_mean is the mean of all training targets.
A category that never appears in training gets global_mean.
Fit on the training data and return the encoding of cats_test.
def target_encode(cats_train, y_train, cats_test, m=1.0):
Input
target_encode(["a", "a", "b"], np.array([1.0, 0.0, 1.0]), ["a", "b", "c"], m=1.0)
Output
array([0.55555556, 0.83333333, 0.66666667])
Reasoning
The global mean is 2 / 3. Category a has 2 rows with mean 0.5, so its
code is (2 * 0.5 + 2/3) / 3 = 0.556. Category b has 1 row with mean 1,
so its code is (1 + 2/3) / 2 = 0.833. Category c is unseen, so it gets
0.667.
m = 0, the code is the raw category mean.cats_test.Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell