




























Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!





























Machine learning is a branch of AI that tries to create models learning from the experience.
data →
result →
machine learning → program
This paradigm is necessary because sometimes conventional programming doesn’t work!
In particular: data – appropriate algorithm → model (used on future data)
So ML is about predicting the future based on the past, i.e. we learn a model from training data and then
we apply the model to testing data to make a prediction.
We can also say that ML is the study of algorithms that improve their performance P at some task T with
experience E.
nodes between input and output) and representing data through multiple levels of abstraction.
Data is a set of examples.
A sample is an unbiased number of observations that is collected.
We need to map data into features, that are generally represented with vectors.
There are difficulties in choosing the “right” features for the given problem i.e. feature descriptor that
lose the minimum of information.
Feature descriptor is a method that extracts the features.
Both the training and the test set are generated based on this distribution.
A probability distribution describes how likely certain events are.
The learning process:
example.
Given a training set 𝒯 = {(𝑥
1
1
𝑚
𝑚
)}, learn a function 𝑓 (model) to predict 𝑦 given 𝑥
such that 𝑓: ℝ
𝑑
𝑦 is categorical (can take on one of a limited, and usually fixed, number of possible values)
A task represents the type of prediction or inference being made to solve a problem on some data.
A task can be identified with ℱ task
, the set of functions that can potentially solve it.
In general it consists of mappings from an input space 𝒳 to an output space 𝒴.
In classification we need to find a function 𝑓 ∊ 𝒴
𝒳
assigning each input 𝑥 ∈ 𝒳 a discrete label, i.e.
1
𝑘
} where 𝑘 is finite.
In regression we need to find a function 𝑓(𝑥) ∊ 𝒴 assigning each input a continuous label.
In particular 𝒴 = ℝ
𝑑
, where 𝑑 is the number of continuous values to predict (when 𝑑 > 1 we talk about
multi-output regression).
Density estimation is about describing the distribution of data points. So we need to find a probability
distribution 𝑓 ∈ 𝛥(𝒳) that fits the data 𝑥 ∈ 𝒳.
In clustering we need to find a function 𝑓 ∈ ℕ
𝒳
that assigns each input 𝑥 ∈ 𝒳 a cluster index 𝑓(𝑥) ∈ ℕ.
All points mapped to the same index form a cluster.
In dimensionality reduction we need to find a function 𝑓 ∊ 𝒴
𝒳
mapping each (high dimensionality) input
𝑥 ∈ 𝑋 to a lower dimensional embedding 𝑓(𝒳) ∈ 𝒴, where dim(𝒴) ≪ dim(𝒳).
Data is information about the problem to solve and is in the form of a distribution, 𝑝
data
This distribution in supervising learning is 𝑝 data
∈ 𝛥(𝒳 × 𝒴), while in unsupervised learning is 𝑝
data
The data distribution 𝑝 data
is typically unknown but we can sample from it → larger training set better
model.
The underline assumption for any ML algorithm to work is that training set, validation set and test set
are sample from the same distribution!
dataset along with improving involved hyperparameters (initially set parameters before the model
begins learning).
A model is like a “program” to solve the problem.
It’s the implementation of a function 𝑓 ∈ ℱ
task
that can be tractably computed.
A set of models forms a hypothesis space ℋ ⊂ ℱ task
The learning algorithm seeks a solution within the hypothesis space.
We want to minimize the generalization error function 𝐸(𝑓; 𝑝 data
), i.e. search the best 𝑓
∗
arg min
𝑓∈ℱ
task
data
The error function determines how well a solution 𝑓 ∈ ℱ task
fits some given data.
We cannot search too much → need to restrict the focus on finding functions that can be implemented
and evaluated in a tractable way.
We define a model hypothesis space ℋ ⊂ ℱ task
and seek a solution within that space, i.e. 𝑓
ℋ
∗
arg min
𝑓∈ℋ
data
But we don’t have access to 𝑝 data
→ need to work on data samples i.e. training set 𝒟
𝑛
1 ,⋯
𝑛
where 𝑧 𝑖
data
. So we need to find 𝑓
ℋ
∗
𝑛
) ∈ arg min
𝑓∈ℋ
𝑛
Typically the generalization and training error functions can be written it terms of a pointwise loss
ℓ(𝑓; 𝑧) measuring the error incurred by 𝑓 on the training example 𝑧, i.e. 𝐸(𝑓; 𝑝
data
𝑧~𝑝
data
We need to implement the error function: 𝐸(𝑓; 𝒟
𝑛
1
𝑛
𝑖
𝑛
𝑖= 1
given the function 𝑓 → we can choose any reasonable measure.
The learning algorithm solves the optimization problem targeting 𝑓 ℋ
∗
𝑛
) but this could be very difficult
(e.g. saddle points, local minimum).
We need to use a training error as a proxy for the generalization error.
The ideal situation is being able to learn a model that works decently both on training and validation.
data → is not able to generalize.
The hypothesis space has a strong impact on how well we can do.
randomness or natural variability in a system. There are deterministic factors that we don’t observe, but
which could be interpreted as inherent variability.
Model complexity matters for overfitting!
the upper limit varies.
𝑘
𝑘
𝑟
𝑛
𝑘= 1
1
𝑟
⁄
where 𝑛 is
the number of dimensions and 𝑟 is a hyper-parameter that has implication on what neighbor is.
In the case of 𝑟 = 1 we talk about Manhattan or L1 norm distance, while in the case of 𝑟 = 2 we’re
referring to Euclidian distance.
The Euclidian and the Minkowski distances are very good metrics when we operate in the vector space,
but in some specific cases there are better options.
1
2
1
2
1
2
This because, after represented each document as a vector indicating how many times a word is present
inside it, it’s easy to say if two documents are discussing the same topic just by looking the angle in
between.
Decision boundaries of k-NN are locally defined and derive from the Voronoi partitions.
segment is equidistant between two points.
K-NN algorithm does not explicitly compute decision boundaries. The decision boundaries form a subset
of the Voronoi diagram for the training data. The more examples that are stored, the more complex the
decision boundaries can become.
The decision boundary change with 𝑘 → larger 𝑘 produces smoother boundary effect but cannot be too
big respect to the size of the training set (e.g. when 𝑘 = 𝑛 always predict the majority class).
How to pick 𝑘 :
𝑛, where 𝑛 is the number of training examples
overfitting) when it’s not given a validation set. It consists in dividing the given dataset in two parts: one
for training and other for validation. Then the model its trained and validated. These steps are done
multiple times.
If we choose as class the majority class within the 𝑘 nearest neighbors, this means that all the samples
weigh the same → suboptimal.
weight assigned.
The weight is inversed proportional to the distance of the points respect to the test sample → this is one
way to compute the weights through exponential decay (of the distance).
K-NN is belongs to the class of lazy learning algorithms.
given a test example.
classify.
K-NN can operate in any dimensions, but it’s severely affected to curse of dimensionality.
The success of k-NN is very dependent on having a dense dataset, where points are close in every single
dimension. There isn’t any procedure to assign more importance to some dimensions rather than others,
so a big dataset is needed.
In practical applications is very rare to use k-NN for very high dimensional problem because inference
time matters!
A solution could be put some preprocessing like feature selection mechanism or dimensionality
reduction mechanism, before applying k-NN.
Computational cost:
small
To build a k-d tree from training data we have to pick a random dimension, find the median, split the
data and repeat.
To find the nearest neighbors for a new point we need to find the region containing the point and then
compare to all points in the region.
K-NN advantages:
K-NN issues:
ML approaches can be categorized in:
performance e.g. learn even with a few training data
to overfitting and require more training data
Where do these assumptions come from? Sometimes domain knowledge.
In k-NN there aren’t model assumptions → k-NN learns everywhere i.e. there aren’t data sets that could
never be learned (suboptimal ≠ can’t learn).
Note that model assumptions (e.g. k-NN none) are different from algorithm design assumptions (e.g. in
k-NN proximity relates to class)!
Classify models:
dimensions or by hyperplanes in 𝑛-dim).
training set D sampled from 𝒟, the goal is to compute a function 𝑓 minimizing 𝔼
(𝑥,𝑦)~𝒟
over 𝒳 × [
, and a training set D sampled from 𝒟, the goal is to compute a function 𝑓 minimizing
(𝑥,𝑦)~𝒟
So multiclass classification is a natural extension of binary classification where the difference is that we
have 𝑘 > 2 classes to choose the labels from.
Most real-world applications involve multiclass predictions!
The k-NN natively can be applied to multiclass classification problem, while the perceptron cannot. To
apply it to multiclass classification problem we need an external procedure that combines multiple lines.
a binary problem: we assign + 1 to the samples of the class 𝑖 and − 1 to the other examples. We repeat
this procedure 𝑘 times i.e. for each class we relabel the training set.
OVA training algorithm:
for 𝑖 = 1 to 𝑘
𝑏𝑖𝑛
← relabel 𝐷
𝑚𝑢𝑙𝑡𝑖𝑐𝑙𝑎𝑠𝑠
so class 𝑖 is positive and ¬𝑖 is negative
𝑖
← BinaryTrain(𝐷
𝑏𝑖𝑛
return 𝑓
1
𝑘
In general classifiers provide confidence: when classifying a sample we pick the most confident positive
or the least confident negative if there aren’t positive votes.
If classifier doesn’t provide confidence and there’s ambiguity, we can simple pick one of the ones in
conflict.
The confidence is the distance from the hyperplane: 𝑝𝑟𝑒𝑑𝑖𝑐𝑡𝑖𝑜𝑛 = 𝑏 + ∑ 𝑤 𝑖
𝑖
𝑛
𝑖= 1
OVA testing algorithm:
for 𝑖 = 1 to 𝑘
𝑖
𝑖
𝑖
return argmax
𝑘
𝑘
Even if OVA is an external procedure to learn multiclass problem, there still are decision boundaries.
consider only two classes at the time: we take out only the examples that correspond to the two classes
from my training set (we have a reduced training set) and we learn the model (binary classification).
AVA training algorithm:
𝑖𝑗
for 𝑖 = 1 to 𝑘 − 1
𝑝𝑜𝑠
← all 𝑥 ∈ 𝐷
𝑚𝑢𝑙𝑡𝑖𝑐𝑙𝑎𝑠𝑠
labeled 𝑖
for 𝑗 = 𝑖 + 1 to 𝑘
𝑛𝑒𝑔
← all 𝑥 ∈ 𝐷
𝑚𝑢𝑙𝑡𝑖𝑐𝑙𝑎𝑠𝑠
labeled 𝑗
𝑏𝑖𝑛
𝑝𝑜𝑠
𝑛𝑒𝑔
𝑖𝑗
← BinaryTrain(𝐷
𝑏𝑖𝑛
return all 𝑓
𝑖𝑗
s
At test time all possible models are evaluated: if the model predicts positive then we assign 1 vote to that
class, otherwise we assign 1 vote to the opponent. After running all the 𝑘(𝑘 − 1 ) ⁄ 2 classifiers, the class
with the most votes wins.
If we want to do something more sophisticated, we can work with weighted votes based on confidence.
AVA testing algorithm:
for 𝑖 = 1 to 𝑘 − 1
for 𝑗 = 𝑖 + 1 to 𝑘
𝑖𝑗
𝑖
𝑖
𝑗
𝑗
return argmax
𝑘
𝑘
AVA performance:
trained on much smaller and balanced data
number of classifiers
When using a binary classifier, the most common thing to do is OVA because it’s more robust in term of
performance.
In real-life problem the perceptron (with these strategies) is not much used because:
We evaluate the performance of models by accuracy, but this measure doesn’t suit well multiclass
classification problem because of the data imbalance.
categories are.
each entry (𝑖, 𝑗) contains the number of examples with label 𝑖 that were predicted to have label 𝑗. A good
model only fills the diagonal.
Perceptron algorithm is one example of a linear classifier, but there are many other algorithms that
learn a line.
Remember the machine learning procedure: we pick a model, we pick an objective function to optimize
and then we develop a learning algorithm that should try to minimize the criteria.
If our model is a linear function we have a hyperplane 0 = 𝑏 + ∑
𝑖
𝑖
𝑛
𝑖= 1
The objective function is the 0/1 loss ∑ 1 [𝑦
𝑖
𝑖
𝑛
𝑖= 1
, where 1 [𝑥] is the indicator function that
maps elements to 1 if 𝑥 = true, and to 0 otherwise.
The learning algorithm aims to find the parameters 𝑏 and 𝑤 such that minimize the number of errors
there are in the training set of size 𝑛 i.e. argmin
𝑤,𝑏
𝑖
𝑖
𝑛
𝑖= 1
It’s hard to minimize the 0/1 loss function because it’s a not differentiable function → each time we
change 𝑤 such that the example is right/wrong the loss will increase/decrease. This turns out to be NP-
hard!
Defining a loss function and calculating the gradient descent on the training set is not enough because
we want the classifier performs well also on test data to avoid overfitting. In general min 𝑤,𝑏
𝐿𝑜𝑠𝑠 on
training and testing set are different!
the model that forces the learning to prefer certain types of weights over others.
Generally, we don’t want huge weights because a small change in a feature can result in a large change
in the prediction, that is suboptimal. We might also prefer weights of 0 for features that are not useful.
So learning algorithm computes argmin
𝑤,𝑏
′
𝑛
𝑖= 1
where 𝜆 is the hyper-
parameter for the trade-off.
Common regularizers:
𝑗
𝑗
2
Note that if the L1 norm is used it’s very likely that the solution will have one feature that will be 0 and
the another not, while with the L2 norm the solution will have small values, both close to 0 but not
exactly 0.
In general all the family of the Lp norm can be adopted as regularizer: 𝑟(𝑤, 𝑏) = √
𝑗
𝑝
𝑝
𝑝
where
larger 𝑝 discourages large weights more.
Since Lp norms for 𝑝 ≥ 1 are convex, we can still use the gradient descent to solve the minimization
problem.
The gradient descent updating rule with the regularization term will be: 𝑤 𝑗
𝑗
𝑑
𝑑𝑤
𝑖
In particular:
𝑗
𝑗
𝑗
𝑗
𝑗
′
𝑗
𝑗
𝑗
′
𝑗
𝑝− 1
There are iterative procedures like gradient descent that can always been applied, however in some
situations there are specific forms of convex optimization problem which have specialized solvers.
Main variations in linear classifiers:
The perceptron algorithm finds some hyperplane that separates the data, but if the data points aren’t
linearly separable it will continue to adjust as it iterates through the examples and the final hyperplane
will depend on which examples it saw recently.
SVM instead, is aiming to find a linear model which attempt to the maximize the margin (this ensures
generalization).
For any separating hyperplane, there exist some set of closest points, called the support vectors. For 𝑛
dimensions, there will be at least 𝑛 + 1 support vectors.
In the final decision model only the support vectors matter, while all the other training examples can be
ignored → efficient at inference time because decision function is computed on a subset of data points.
The margin of a hyperplane is measured as 1 ⁄‖ 𝑤‖, so to maximize it the objective function must be
max 𝑤,𝑏
1
||𝑤||
. But this formula doesn’t consider our data points! To do that we have to put a constraint:
𝑖
𝑖
are correctly classified (data are separable) and outside the margin.
Maximizing the margin is equivalent to minimize the norm of the weights, so instead of max 𝑤,𝑏
1
|
| 𝑤
| |
it’s
better to use min
𝑤,𝑏
2
, a more convenient optimization problem.
To extend this problem in the case of not linearly separable data points we need to tolerate some error
using the soft margin classification.
The objective function is min 𝑤,𝑏
2
𝑖 𝑖
subject to 𝑦
1
𝑖
𝑖
𝑖
≥ 0 , where 𝜍
𝑖
are
the so called slack penalties that tell how far from “correct” are each point.
In particular, the slack values for points outside (or on) the margin and correctly classified are 0, while
for points incorrectly classified is the distance to the margin:
𝑖
0 i𝑓 𝑦
𝑖
𝑖
𝑖
𝑖
𝑖
= max( 0 , 1 − 𝑦
𝑖
𝑖
′
C is a regularization parameter that trades off the relative importance of maximizing the margin and
fitting the training data i.e. it controls overfitting.
In general if C is small we have large margin (constraints can be easily ignored), if C is large we have
narrow margin, and if C = ∞ we have hard margin.
A good value of C depends on validation set so to find the optimal hyper-parameter we have to solve the
quadratic optimization problem multiple times and see the performance of it.
SVM can also been seen as an unconstrained optimization problem since is possible to bring constraints
into the objective function: min
𝑤,𝑏
2
𝑖
𝑖
𝑖
Note that the objective function can be traced back to argmin
𝑤,𝑏
′
𝑛
𝑖= 1
by imposing the loss to be the hinge loss max( 0 , 1 − 𝑦𝑦
′
) and 𝜆 = 1 ⁄𝐶 , so SVM can be solved with
gradient descent too.
Most of the problems are not-linearly separable, so it’s always convenient to choose a non-linear kernel
instead of a linear one.
The most common choice for data that are vectors, it’s the gaussian kernel that map the data into an
infinite-dimensional space.
SVMs can be applied to complex data beyond feature vectors by designing kernel functions for such
data.
Concluding, a non-linear SVM problem is about solving the dual optimization problem
max
𝛼
𝑖 𝑖
1
2
𝑖
𝑗
𝑖
𝑗
𝑇
𝑖
𝑗 𝑖 𝑗
such that ∑ 𝛼
𝑖
𝑖
𝑖
𝑖
, so the solution is the
classifying function 𝑓 (
𝑖
𝑖
𝑖
𝑖
Note that the optimization techniques for finding 𝛼 𝑖
’s remain the same!
SVM power:
In some classification problems we could have exclusive classes, while in others there could be some
form of hierarchy or some general structure between the labels.
A model that exploit these relationships between classes can help to reduce the error.
from N possible classes.
Multilabel classification problems can be solved by computing separate linear models (one for each
attribute of the identification task). However most of the sophisticated machine learning methods
perform multitask learning.
Assuming to have 𝑁 queries and 𝑀 samples in the training set, the goal of ranking is to train a binary
So given a query 𝑞 and two samples 𝑥 𝑖
and 𝑥
𝑗
, the classifier should predict whether 𝑥
𝑖
should be
preferred to 𝑥 𝑗
with respect to the query 𝑞.
Note that a binary classifier takes as input a vector that is associated to a test sample, then outputs + 1
or − 1 (or optionally a confidence/score). But how can it predict preferences to turns them into a
ranking?
An idea is to train a classifier to decide if the first input is better than second.
So given some ranking, we can create an appropriate training set by considering all possible pairings of
the examples in a ranking and labeling them as positive if the first is higher ranked, negative otherwise.
Binary classifier only takes one input, so we have to create a new vector representation for each pair
made of features that aggregate information between samples.
Common approaches:
𝑖
′
𝑖
𝑖
𝑖
′
𝑖
𝑖
Ranking training algorithm:
for 𝑛 = 1 to 𝑁
for all 𝑖, 𝑗 = 1 to 𝑀 and 𝑖 ≠ 𝑗
if 𝑖 is preferred to 𝑗 on query 𝑛
𝑛𝑖𝑗
else if 𝑗 is preferred to 𝑖 on query 𝑛
𝑛𝑖𝑗
return BinaryTrain(𝐷)
At test time we receive some new vectors of features, and to produce a ranking we again consider all the
possible pairings, extract the features and then apply the binary classifier.
After that each binary sample has a score based on the prediction, we have to call a sorting algorithm to
recover the ranking.
Testing algorithm:
for all 𝑖, 𝑗 = 1 to 𝑀 and 𝑖 ≠ 𝑗
𝑖𝑗
𝑖
𝑖
𝑗
𝑗
return ArgSort(𝑠𝑐𝑜𝑟𝑒)
Note that this simple ranking routine doesn’t take into account how far two objects are (in the ranking)
ahead of all Bs (predict binary response).
distance in ranking.
If the classifier outputs a confidence, we can sort the examples using it as the similarity between
examples.
Another improvement is to use a preference function as sorting function.
In this case we define a ranking as a function 𝜎 that maps the objects we are ranking to the desired
position in the list: 1 , 2 , … , 𝑀. So given data with observed rankings 𝜎, our goal is to learn to predict
rankings for new objects, 𝜎
∗
If we also want to model the fact that making a mistake on some pairs is worse than making a mistake
on others, we need to define a cost function 𝜔, where 𝜔(𝑖, 𝑗) is the cost for accidentally putting
something in position 𝑗 when it should have gone in position 𝑖.
A valid cost function 𝜔 must be symmetric, monotonic and satisfy the triangle inequality.
Depending on the problem the cost function may be defined in different ways:
= 1 whenever 𝑖 ≠ 𝑗 → simply counts the number of pairwise mis-ordered items
1 if min{𝑖, 𝑗} ≤ 𝑘 and 𝑖 ≠ 𝑗
0 otherwise
→ impose the top 𝑘 predictions correct
𝑀
(where 𝛴
𝑀
is the set
of all ranking functions over M objects), a training set D sampled from 𝒟, we want to compute a function
𝑀
minimizing 𝔼
(𝑥,𝜎)~𝒟
𝑢
𝑣
𝑣
𝑢
𝑢
𝑣
𝑢≠𝑣
] where 𝜎̂ = 𝑓(𝑥).
Ranking improved training algorithm:
𝑏𝑖𝑛
for all (𝑥, 𝜎) ∈ 𝐷
𝑟𝑎𝑛𝑘
for all 𝑢 ≠ 𝑣
𝑦 ← Sign(𝜎
𝑣
𝑢
𝑣
𝑢
𝑏𝑖𝑛
𝑏𝑖𝑛
𝑢𝑣
return BinaryTrain(𝐷
𝑏𝑖𝑛
The tree-growing strategy recursively partitions the training set and decides whether to grow leaves or
non-terminal nodes.
In general we fix a set of leaf predictions ℋ
𝑙𝑒𝑎𝑓
𝑡𝑎𝑠𝑘
, and a set of possible routing (or split)
functions 𝜙 ⊂ {𝐿, 𝑅}
𝒳
If 𝒟 = {𝑧 1
𝑛
} is the training set reaching the node than the optimal leaf predictor can be computed
as ℎ
𝒟
∗
∈ arg min
ℎ∈ℋ
𝑙𝑒𝑎𝑓
The optimal error value is also called impurity measure 𝐼(𝒟) = 𝐸(ℎ
𝒟
∗
; 𝒟) and it indicates the amount of
elements that isn’t on the associated class.
Pure set → set with homogeneous samples (same class).
Impure set → group of elements of different “types”.
We grow a leaf 𝐿𝑒𝑎𝑓(ℎ
𝒟
∗
) if some criterion like these is met:
If no stopping criterion is met we need to find an optimal split function 𝜙 𝒟
∗
∈ arg min
𝜙∈𝛷
𝜙
The impurity 𝐼 𝜙
(𝒟) of a split function 𝜙 given a training set 𝒟 is computed in terms of the impurity of
the split data: 𝐼
𝜙
|𝒟
𝑑
𝜙
|
| 𝒟
𝑑∈{𝐿,𝑅} |
𝑑
𝜙
) where 𝒟
𝑑
𝜙
To recap how to create a tree:
𝒟
∗
) if stopping criterion met
𝒟
∗
𝐿
∗
𝑅
∗
)) otherwise
Sometimes the selection of the best split function is not given in terms of minimizing the impurity of the
split, but in the equivalent maximization of information gain, which is given by ∆ 𝜙
𝜙
The information gain is non-negative, i.e. ∆ 𝜙
(𝒟) ≥ 0 for any 𝜙 ∈ {𝐿, 𝑅}
𝒳
and any training set 𝒟 ⊂ 𝒳 × 𝒴,
so with this criterion the impurity will never increase for any randomly chosen split.
The leaf prediction can be an arbitrary function ℎ ∈ ℱ task
, but in practice we restrict it to a subset
𝑙𝑒𝑎𝑓
of simple ones.
The simplest predictor is a function returning a constant i.e. ℎ ∈ ℋ
𝑙𝑒𝑎𝑓
𝑦∈𝒴
𝒳
For classification we consider as error function 𝐸(𝑓; 𝒟) =
1
|𝒟|
𝑧∈𝒟
If ℓ(𝑓; (𝑥, 𝑦)) = 1 𝑓(𝑥)≠𝑦
and ℋ
𝑙𝑒𝑎𝑓
𝑦∈𝒴
𝒳
than the impurity measure is the classification error:
𝐼(𝒟) = 1 − max
𝑦∈𝒴
|𝒟
𝑦
|
|𝒟|
where 𝒟
𝑦
′
′
} denote the subset of training samples in 𝒟 with
class label 𝑦.
But we can do different choices for the loss and the leaf function:
If ℓ(𝑓; (𝑥, 𝑦)) = − log 𝑓
𝑦
(𝑥) and ℋ
𝑙𝑒𝑎𝑓
𝜋∈∆(𝒴)
𝒳
(constant label distribution as leaf
prediction) than the impurity measure is the entropy: 𝐼(𝒟) = − ∑
|𝒟
𝑦
|
|𝒟|
log
|𝒟
𝑦
|
|𝒟|
𝑦∈𝒴
If ℓ(𝑓; (𝑥, 𝑦)) = ∑ [𝑓
𝑐
𝑐=𝑦
2
𝑐∈𝒴
and ℋ
𝑙𝑒𝑎𝑓
𝜋∈∆(𝒴)
𝒳
than the impurity measure is
the Gini impurity: 𝐼 (
|𝒟
𝑦
|
| 𝒟
|
2
𝑦∈𝒴
If ℓ(𝑓; (𝑥, 𝑦)) = ‖𝑓(𝑥) − 𝑦‖
2
and ℋ
𝑙𝑒𝑎𝑓
𝑦∈𝒴
𝒳
than the impurity measure is the variance:
1
|𝒟|
𝒟
2
(𝑥,𝑦)∈𝒟
where 𝜇
𝒟
1
|𝒟|
(𝑥,𝑦)∈𝒟
The routing or split 𝜙 ∈ {𝐿, 𝑅}
𝒳
determines whether a data point 𝑥 ∈ 𝒳 should move left or right.
The possible split functions is restricted to some predefined set 𝛷 ∈ {𝐿, 𝑅}
𝒳
depending on the nature of
the feature space.
If we assume discrete nominal features taking values in 𝒦, than the split function can be implemented
given a partition of 𝒦 into 𝒦 𝑅
and 𝒦
𝐿
as 𝜙(𝑥) = {
𝐿 if 𝑥 ∈ 𝒦
𝐿
𝑅 if 𝑥 ∈ 𝒦
𝑅
Finding the optimal split requires testing 2
|𝒦|− 1
− 1 bi-partitions.
If we assume ordinal features (i.e. can be sorted) taking values in 𝒦, than the split function can be
implemented given a threshold 𝑟 ∈ 𝒦 as 𝜙(𝑥) = {
𝐿 if 𝑥 ≤ 𝑟
𝑅 if 𝑥 > 𝑟
Note that if |𝒦| ≤ |𝒟|, finding the optimal split requires testing |𝒦| − 1 thresholds. Otherwise, it requires
sorting the input values in 𝒟, where 𝒟 is the training set reaching the node, and testing |𝒟| − 1
thresholds.
Sometimes it’s convenient to split using multiple features at once. Such split functions work with
continuous features and are called oblique, because can generate oblique decision boundaries.
If 𝑥 ∈ ℝ
𝑑
than the split function can be implemented given 𝑤 ∈ ℝ
𝑑
and 𝑟 ∈ ℝ as 𝜙(𝑥) = {
𝐿 if 𝑤
𝑇
𝑅 otherwise
Note that this type of functions allow to have more expressivity but lose efficiency because it’s harder to
optimize.
Basic algorithm pseudocode:
node = root of DT
Main loop:
Else, recurse over new leaf nodes
Decision trees are very convenient because they are explainable models: at the end we know we have
predicted a certain label because we have certain features activated → non-trivial.
However, since we are trying to reach the minimum error on the training set, we are very likely to incur
into overfitting.
Strategies to improve generalization:
Problems of DT:
possible
(i.e. more robust to overfitting) by combining the predictions from multiple models.
The general idea is to create small datasets from the original one, learn a model for each of these, and
then aggregate them to produce the final model.