machine studying algorithms can’t deal with categorical variables. However resolution bushes (DTs) can. Classification bushes don’t require a numerical goal both. Beneath is an illustration of a tree that classifies a subset of Cyrillic letters into vowels and consonants. It makes use of no numeric options — but it exists.
Many additionally promote imply goal encoding (MTE) as a intelligent method to convert categorical knowledge into numerical type — with out inflating the function house as one-hot encoding does. Nevertheless, I haven’t seen any point out of this inherent connection between MTE and resolution tree logic on TDS. This text addresses precisely that hole via an illustrative experiment. Particularly:
- I’ll begin with a fast recap of how Decision Trees deal with categorical options.
- We’ll see that this turns into a computational problem for options with excessive cardinality.
- I’ll reveal how imply goal encoding naturally emerges as an answer to this drawback — not like, say, label encoding.
- You’ll be able to reproduce my experiment utilizing the code from GitHub.
A fast word: One-hot encoding is commonly portrayed unfavorably by followers of imply goal encoding — nevertheless it’s not as dangerous as they counsel. In reality, in our benchmark experiments, it usually ranked first among the many 32 categorical encoding strategies we evaluated. [1]
Determination bushes and the curse of categorical options
Determination tree studying is a recursive algorithm. At every recursive step, it iterates over all options, trying to find one of the best cut up. So, it’s sufficient to look at how a single recursive iteration handles a categorical function. If you happen to’re not sure how this operation generalizes to the development of the complete tree, have a look right here [2].
For a categorical function, the algorithm evaluates all attainable methods to divide the classes into two nonempty units and selects the one which yields the very best cut up high quality. The standard is often measured utilizing Gini impurity for binary classification or imply squared error for regression — each of that are higher when decrease. See their pseudocode under.
# ---------- Gini impurity criterion ----------
FUNCTION GiniImpurityForSplit(cut up):
left, proper = cut up
complete = measurement(left) + measurement(proper)
RETURN (measurement(left)/complete) * GiniOfGroup(left) +
(measurement(proper)/complete) * GiniOfGroup(proper)
FUNCTION GiniOfGroup(group):
n = measurement(group)
IF n == 0: RETURN 0
ones = rely(values equal 1 in group)
zeros = n - ones
p1 = ones / n
p0 = zeros / n
RETURN 1 - (p0² + p1²)
# ---------- Imply-squared-error criterion ----------
FUNCTION MSECriterionForSplit(cut up):
left, proper = cut up
complete = measurement(left) + measurement(proper)
IF complete == 0: RETURN 0
RETURN (measurement(left)/complete) * MSEOfGroup(left) +
(measurement(proper)/complete) * MSEOfGroup(proper)
FUNCTION MSEOfGroup(group):
n = measurement(group)
IF n == 0: RETURN 0
μ = imply(Worth column of group)
RETURN sum( (v − μ)² for every v in group ) / n
Let’s say the function has cardinality ok. Every class can belong to both of the 2 units, giving 2ᵏ complete combos. Excluding the 2 trivial instances the place one of many units is empty, we’re left with 2ᵏ−2 possible splits. Subsequent, word that we don’t care concerning the order of the units — splits like {{A,B},{C}} and {{C},{A,B}} are equal. This cuts the variety of distinctive combos in half, leading to a remaining rely of (2ᵏ−2)/2 iterations. For our above toy instance with ok=5 Cyrillic letters, that quantity is 15. However when ok=20, it balloons to 524,287 combos — sufficient to considerably decelerate DT coaching.
Imply goal encoding solves the effectivity drawback
What if one may cut back the search house from (2ᵏ−2)/2 to one thing extra manageable — with out dropping the optimum cut up? It seems that is certainly attainable. One can present theoretically that imply goal encoding permits this discount [3]. Particularly, if the classes are organized so as of their MTE values, and solely splits that respect this order are thought-about, the optimum cut up — in accordance with Gini impurity for classification or imply squared error for regression — will probably be amongst them. There are precisely k-1 such splits, a dramatic discount in comparison with (2ᵏ−2)/2. The pseudocode for MTE is under.
# ---------- Imply-target encoding ----------
FUNCTION MeanTargetEncode(desk):
category_means = common(Worth) for every Class in desk # Class → imply(Worth)
encoded_column = lookup(desk.Class, category_means) # substitute label with imply
RETURN encoded_column
Experiment
I’m not going to repeat the theoretical derivations that help the above claims. As a substitute, I designed an experiment to validate them empirically and to get a way of the effectivity positive factors introduced by MTE over native partitioning, which exhaustively iterates over all attainable splits. In what follows, I clarify the info technology course of and the experiment setup.
Knowledge
To generate artificial knowledge for the experiment, I used a easy perform that constructs a two-column dataset. The primary column accommodates n distinct categorical ranges, every repeated m occasions, leading to a complete of n × m rows. The second column represents the goal variable and will be both binary or steady, relying on the enter parameter. Beneath is the pseudocode for this perform.
# ---------- Artificial-dataset generator ----------
FUNCTION GenerateData(num_categories, rows_per_cat, target_type='binary'):
total_rows = num_categories * rows_per_cat
classes = ['Category_' + i for i in 1..num_categories]
category_col = repeat_each(classes, rows_per_cat)
IF target_type == 'steady':
target_col = random_floats(0, 1, total_rows)
ELSE:
target_col = random_ints(0, 1, total_rows)
RETURN DataFrame{ 'Class': category_col,
'Worth' : target_col }
Experiment setup
The experiment
perform takes an inventory of cardinalities and a splitting criterion—both Gini impurity or imply squared error, relying on the goal sort. For every categorical function cardinality within the checklist, it generates 100 datasets and compares two methods: exhaustive analysis of all attainable class splits and the restricted, MTE-informed ordering. It measures the runtime of every technique and checks whether or not each approaches produce the identical optimum cut up rating. The perform returns the variety of matching instances together with common runtimes. The pseudocode is given under.
# ---------- Cut up comparability experiment ----------
FUNCTION RunExperiment(list_num_categories, splitting_criterion):
outcomes = []
FOR ok IN list_num_categories:
times_all = []
times_ord = []
REPEAT 100 occasions:
df = GenerateDataset(ok, 100)
t0 = now()
s_all = MinScore(df, AllSplits, splitting_criterion)
t1 = now()
t2 = now()
s_ord = MinScore(df, MTEOrderedSplits, splitting_criterion)
t3 = now()
times_all.append(t1 - t0)
times_ord.append(t3 - t2)
IF spherical(s_all,10) != spherical(s_ord,10):
PRINT "Discrepancy at ok=", ok
outcomes.append({
'ok': ok,
'avg_time_all': imply(times_all),
'avg_time_ord': imply(times_ord)
})
RETURN DataFrame(outcomes)
Outcomes
You’ll be able to take my phrase for it — or repeat the experiment (GitHub) — however the optimum cut up scores from each approaches all the time matched, simply as the speculation predicts. The determine under exhibits the time required to judge splits as a perform of the variety of classes; the vertical axis is on a logarithmic scale. The road representing exhaustive analysis seems linear in these coordinates, that means the runtime grows exponentially with the variety of classes — confirming the theoretical complexity mentioned earlier. Already at 12 classes (on a dataset with 1,200 rows), checking all attainable splits takes about one second — three orders of magnitude slower than the MTE-based method, which yields the identical optimum cut up.

Conclusion
Determination bushes can natively deal with categorical knowledge, however this capacity comes at a computational price when class counts develop. Imply goal encoding presents a principled shortcut — drastically decreasing the variety of candidate splits with out compromising the end result. Our experiment confirms the speculation: MTE-based ordering finds the identical optimum cut up, however exponentially sooner.
On the time of writing, scikit-learn
doesn’t help categorical options immediately. So what do you assume — if you happen to preprocess the info utilizing MTE, will the ensuing resolution tree match one constructed by a learner that handles categorical options natively?
References
[1] A Benchmark and Taxonomy of Categorical Encoders. In the direction of Knowledge Science. https://towardsdatascience.com/a-benchmark-and-taxonomy-of-categorical-encoders-9b7a0dc47a8c/
[2] Mining Guidelines from Knowledge. In the direction of Knowledge Science. https://towardsdatascience.com/mining-rules-from-data
[3] Hastie, Trevor, Tibshirani, Robert, and Friedman, Jerome. The Parts of Statistical Studying: Knowledge Mining, Inference, and Prediction. Vol. 2. New York: Springer, 2009.