




























































































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 Notes
1 Introduction
Possible definition of target function V:
′ ), where b
′ is the best final board state that can be achieved
starting from b and playing optimally until the end of the game
In this code c is a small constant used to moderate the rate of learning.
Algorithm 1 LMS Weight update rule
1 : Initialize w i
2 : Do repeatedly:
3 : Select a training example b at random
4 : Compute error(b):
5 : error(b) = Vtr ain(b) −
V(b)
6 : For each board feature fi , update weight wi :
7 : wi ← wi + c · fi · error(b)
General machine learning problem:
f that returns values as close as possible to
f , specially for samples x not present in the training set D
There are 3 main categories for ML problems and what is different in the ML problems categories is how data
is represented.
Is a set of problems in which the dataset is formed by a set of pairs input-output (D = {< xi, yi >}), so if I take an
element from X I know the corresponding value in Y.
D ⊂ {(x, y)|x ∈ X, y ∈ Y };
X ≡ A 1 × ... × An, Ai finite sets (Discrete)
n (Continuous)
Y ≡ {C 1 , ..., Ck }, (Classification)
k (Regression)
In supervised learning, you train the machine which is well ”labeled”. It means that some data is already tagged
with the correct answer.
In this case we have Classification (aka Pattern Recognition) in which we have the task of learning a function that
has a finite codomain. It returns the class to which a specific instance belong.
Regression is the task of predicting a single output value using training data. We have the task of learning a
function whose domain output is infinite and has the goal to approximate real-valued functions.
Fundamentally, classification is about predicting a label and regression is about predicting a quantity.
Machine Learning Notes
! LucasMac!
2 Classification Evaluation
If any subset of the instances can be represented in a version space and all the solution are computed (search is
completed) then the system is not able to classify new instances. In fact, different hypotheses in a version space may
return different values for a new instance x
′ ! D.
Collecting data is difficult and it’s easy to make some mistakes because data set may contain noisy data and so the
output is different from the true value of the target function. D = {< xi, yi } with yi " c(xi ) for some i. This can be
cause by sensor errors, approximations and human errors and we need statical methods to remove the noise.
To introduce the notion of performance metrics in ML we have to define a probability distribution of all the possible
inputs. Consider a typical classification problem:
f : X → Y
D : Probability distribution over X.
S : Sample of n instances drawn from X (according to distribution D) and for which we know f (x)
D is the distribution over the dataset that represent the probability that an element is an element of a class.
Performance Evaluation is based on accuracy and error rate. We can have two definitions of error/accuracy:
will misclassify an instance according to D:
error D
(h) ≡ Pr
x ∈D
[ f (x) " h(x)]
This error can not be computed because X sometimes is infinite and we don’t know f (x).
mistakes that h makes on S:
errorS (h) ≡
n
x ∈S
δ( f (x) " h(x))
where δ( f (x) " h(x)) is equal to 1 if f (x) " h(x) and 0 otherwise.
The sample error is the number of mistakes that h makes on a data sample, it can be computed cause for the
samples of S we know the values of the target function. The problem is: this not enough, because we cannot
be sure that this will be the ”resulting error” of the entire distribution. The sample error is computed only on
a small data sample.
accuracy(h) ≡ 1 − error(h)
So, the true error can not be computed (because I don’t have f (x) for each x ∈ X) and the sample error is
computed only on a small data sample. We need to remember that the goal of a learning system is to be accurate
in h(x), ∀x! S and if accuracy S
(h) is very high but accuracy D
(h) is poor our system wold not be very useful.
We call bias the difference between the expected value of the sample error and the true error. Estimator bias:
bias ≡ E[errorS (h)] − erroD (h)
We want to obtain a bias of 0 , with this value in fact we can approximate the true error with the expected
value of the sample error.
E[errorS (h)] = errorD (h)
expected variance and bigger is S, lower will be the difference between twi errors.
Machine Learning Notes
! LucasMac!
Confidence interval: With some probability the true error is in an interval defined by the sample error, where these
2 values are defined by a coefficient zN. If S contains n examples (n ≥ 30 ) drawn independently of h and each other,
with approximately N% probability errorD (h) lies in interval:
errors (h) ± zN
error S
(h)( 1 − error S
(h))
n
where
z N
This means that increasing the size of S, n increases and decreases the confidence interval that approaches to error S
In order to compute error S
, we need to:
(h) =
1
n
x ∈S
δ( f (h) " h(x))
errorS (h) is a random variable and in as unbiased estimator for errorD (h).
Now we want to compare two different hypothesis (d ≡ errorD (h 1 ) − errorD (h 2 )) because we can’t compute the true
difference and we have to estimate it with the sampled difference
d ≡ errorS 1
(h 1 ) − errorS 2
(h 2 ), E[
d] = d
d is an unbiased estimator for d which tell us that i take h 1 if
d is negative, otherwise i choose h 2. Note, you can
use different S 1 and S 2 because they are independent.
Consider error of hypothesis h over
Overfitting
Hypothesis h ∈ H overfits training data if there is an alternative hypothesis h
′ ∈ H that
error S
(h) < error S
(h
′ ) and error D
(h) > error D
(h
′ )
So, in the overfitting we have that the sample error of h is lower than the sample error of h
′ but the true error is
greater, so we have a problem. Taking the best solution which minimize the error on S is not always the best solution
(it may be overfitting). h is the solution of learning algorithm L when using a training set T
h = L(T)
We can evaluate a hypothesis h by estimating the error on different samples S:
(h) is the result of only one experiment and the confidence interval can be large.
(h) for different independent sample data S i
K-Fold Cross Validation method
Partition data set D into k disjoint sets S 1 , S 2 , ..., Sk (|Si | > 30 )
For i = 1 , ..., k do use Si as test set and the remaining data as training set Ti
← error Si
(h i
errorL,D ≡
k
k "
i= 1
δi
Note: accuracyL,D = 1 − errorL,D.
Repeating the loop k times, I just compute the average of all the errors and I get the best approximation of
the true error. If k is too small, the average will be not good (in general k = 30 is good enough.
Machine Learning Notes
! LucasMac!
3 Decision Trees
The problem we are concentrating on is: ”Given a training set D for a target function c, compute consistent hy-
potheses with respect to D”.
Decision Tree = tree where each node represent a test on attribute and branches assign values to the attribute.
leaves are the result of the classification.
The approach to solve it can be:
We are going to considerate an hypotheses space made by the set of decision tree; this means that every hypothesis
is a decision tree.
A decision tree can be transformed in a set of rules, it represents the disjunction of conjunction of all the paths to
the positive (true) leaf nodes. So, if we have for example this decision tree:
we obtain the following rules:
(Outlook = Sunny ∧ Humidit y = Normal) ∨ (Outlook = Overcast) ∨ (Outlook = Rain ∧ Wind = W eak)
In this simple example, internal nodes represent a test of attribute and each brace represent an assignment. So, we
have an ”OR” for each edge from the root and we have an ”AND” for each internal node in the path.
To compute consistent hypothesis we don’t want to compute all the possible ones. There are many decision trees in
the space, maybe with different order of attributes. Given a database, we want to build a tree consistent with it and
which has the best performance.
ID 3 Algorithm
In decision tree learning, ID 3 (Iterative Dichotomiser 3 ) is an algorithm used to generate a decision tree from
a dataset and is typically used in the machine learning and natural language processing domains.
1 ) Create Root node for the tree
2 ) if all Examples are positive, then return the node Root with label +
3 ) if all Examples are negative, then return the node Root with label −
4 ) if Attributes is empty, then return the node Root with label = most common value of T arget attribute
in Examples
5 ) Otherwise:
of A
= subset of E xamples that have value v i
for A
is empty then add a leaf node with label =
most common value of Target attribute in Examples
add the tree ID 3 (E xamples vi
, T arget attribute, Attributes − {A})
Machine Learning Notes
! LucasMac!
Explanation of the Algorithm
If we want to create a decision tree from a dataset D, we have to choose as input:
As output we want one consistent decision tree, who is built in a recursive way:
4 th case that is the recursive case. In first 3 cases the root node would be a leaf that will be labelled with one
possible classification method, the other cases are intermediate, we named after an attribute.
generate a tree that contains only one leaf node that is labelled with +.
generate a tree that contains only one leaf node that is labelled with −.
attribute in Examples. This will happen cause at some point the recursion will reduce the set of attributes.
If it’s empty, we are going to create a leaf, this is an important feature of decision tree. Neither all examples
are positive neither negative it uses statistics (the most common value), and this happened when we have a
noisy dataset ( =⇒ not consistent hypothesis), algorithm is able to generate a solution even in this case (return
anyway a decision tree).
we generate all branches associated. Now considering the subset of Examples that contains the branches of
the best attribute. If this subset is empty like before we generate a leaf node with a label equal to the most
common value of Target attribute in Examples. Else we call the recursive algorithm by passing it, the new
subset of Examples and the list of the Attributes without the best attribute used.
If the dataset is consistent, the choice based on most common value will never happen, in fact there will be
never any conflict. This algorithm will produce consistent data set whatever is the choice of the attribute.
We need to choose the best attribute because it will increase the accuracy of the tree. ID 3 select the attribute with
the highest information gain.
Information gain measures how well a given attribute separates the training examples according to their target
classification.
Entropy is associated to a binary dataset and is nothing but the measure of disorder. Let’s call
Entropy measures the impurity of S:
Entropy(S) ≡ −p⊕ log 2
p⊕ − p⊖ log 2
p⊖
So, Entropy is max when the dataset contains exactly half positive and half negative, so when p ⊕
= p ⊖
= 0. 5. The
idea is to reduce entropy partitioning the dataset and going to 0 or to 1.
Machine Learning Notes
! LucasMac!
3 Decision Trees
some case we can find out that later this is not a good choice and you can’t go back;
ensure robustness to noisy data and robustness to inconsistencies (also important for improving performance
in terms of accuracy).
and therefore also the whole tree, need to redo again (can change completely).
To consider continuous attributes we use intervals in order to have discrete attributes (to cycle on the passible values).
There are some studies in which info gain is not a good choice, depending for example on the number of values of
the attributes, then we have to use different performance metrics still based on reduction of entropy.
Consider a new data set D
′ = D ∪ d 15
adding a training example:
d 15
=< Sunny, Hot, Normal, Strong, PlayT ennis = No >
ID 3 will generate a different tree T
′
. Note: T is consistent with D and T
′ is consistent with D
′ .
accuracy D
′ ) > accuracy D
Is not positive that the tree grows over time to ”accommodate” all possible instances because can produce overfitting.
In fact it could happen that even with a better accuracy computed on the dataset used for training we may have
that the accuracy on test dataset decrease.
The accuracy on the training data tends to grow, but the accuracy on the sample outside of the dataset is increased
up to some point (till a size of the tree) then it decreases. The biggest tree is perfectly shaping the training set.
To avoid overfitting we try to do this two things:
performance, or
growing the full tree means that we remove some branches.
To determine the correct tree size:
use this test set to check if remove that branch lead to an improvement of accuracy.
I need an approach like cross validation and to understand if the cut would be useful we have to estimate if the cut
will improve the accuracy.
So, before I start the process we partition the dataset in training and, for evaluating the accuracy of the tree,
validation. This is called Reduced-Error Pruning, in which we replace subtrees with the more common values in
order to improve accuracy on the validation set. Repeat until any cut on the tree would decrease the accuracy.
Machine Learning Notes
! LucasMac!
3 Decision Trees
Do until further pruning is harmful (harmful = dannoso) (decreases accuracy):
Remove all the subtree and assign the most common classification
Greedily remove the one that most improves validation set accuracy
Notes:
ID 3 is suboptimal, and pruning approach is greedy)
If Attributes have continuous values, we can use intervals in order to have discrete attributes. Else, if we have
attributes with many values or with a cost we can use different performance metrics (not gain) but still based on the
reduction of the entropy. If for some samples we have missing values of an attribute there are statistical methods to
generate a solution without losing the information.
their values into a final result. It is a method that generates a set of decision trees using random criteria (e.g.
random picking attributes) and integrates their values into a final value. The result of the Random Forest is
the most common classification of all the trees (majority vote) and they are less sensitive to overfitting.
random subsets of data (bagging)
random subset of attributes (feature selection)
Machine Learning Notes
! LucasMac!
Given these variables we can define propositions: easy for Boolean variable (it itself is a proposition), for discrete
random variable we can use equality constrains, for continuous one equality or inequality. We can combine these
propositions with Boolean operators.
Prior or unconditional probabilities of propositions correspond to belief prior to arrival of any (new) evidence. It’s
the probability of an event with knowing any other information (e.g. (Odd = true) = 0. 5 ).
A probability distribution is the set of probability values for all the possible assignment a random variable (the sum
of all values must be 1 ), for continuous variables the probability distribution is a continuous function.
Joint probability distribution for a set of random variables gives the probability of every atomic joint event on those
random variables (i.e., every sample point in the joint sample space).
Notes:
values of the variable. This extend also to continuous probabilities distributions (if one of the R v
s is continuous,
also the joint probability distribution is continuous)
possible information of our process.
I know the outcome of a random variable, how this affect probability of other random variables? Conditional
probability is the probability of an event given another event happened, it specifies how the new information affects
the random variable.
In general, conditional/posterior probabilities are different from joint probabilities and from prior probabilities:
P(Cavit y = true|W eather = sunny) " P(Cavit y = true, W eather = sunny) " P(Cavit y = true)
Conditional probability distributions: representation of all the values of a conditional probabilities of random vari-
ables.
Definition of Conditional Probability:
P(a|b) ≡
P(a ∧ b)
P(b)
if P(b) " 0
product rule P(a ∧ b) = P(a|b)P(b) = P(b|a)P(a)
For a boolean random variable B:
P(a) = P(a|b)P(b) + P(a|┐b)P(┐b)
In general for a random variable Y accepting mutually exclusive values y i
y i
∈D(Y)
P(X |Y 0 yi )P(Y = yi ), where D(Y ) = set of values for variable Y
Is derived by successive application of product rule:
P(X 1 , ..., Xn) =
n (
i= 1
P(Xi |X 1 , ..., Xi− 1 )
A and B are independent if and only if:
P(A|B) = P(A) or P(B| A) = P(B) or P(A, B) = P(A)P(B)
Absolute independence powerful, but rare. Complex systems have hundreds of variables, none of which are indepen-
dent.
Machine Learning Notes
! LucasMac!
4 Probability and Bayes Networks
Variables are conditional independent each other when something happens. General formulation: X conditionally
independent from Y given Z iff P(X |Y, Z) = P(X |Z).
P(Y 1 , ..., Yn |Z) = P(Y 1 |Y 2 , ...Yn, Z)P(Y 2 |Y 3 ...Yn, Z)P(Yn |Z)
Yi conditionally independent from Yj given Z
1
n
1
2
n
Chain rule + Conditional independence:
In most cases, the use of conditional independence reduces the size of the representation of the joint distribution
from exponential in n to linear in n.
Product rule P(a ∧ b) = P(a|b)P(b) = P(b|a)P(a)
=⇒ Bayes’ rule P(a|b) =
P(b|a)P(a)
P(b)
or in distribution form:
= αP(X |Y )P(Y )
Useful for assessing diagnostic probability from causal probability:
P(Cause|E f f ect) =
P(E f f ect|Cause)P(Cause)
P(E f f ect)
Bayes rule: P(Z |Y 1 , ..., Y n
) = αP(Y 1
n
i
n
conditionally independent each other given Z
1
n
) = αP(Y 1
n
Effects conditionally independent each other given a cause.
P(Cause|E f f ect 1
, ..., E f f ect n
) = αP(Cause)
i
P(E f f ect i
|Cause)
Total number of parameters is linear in n.
Given a set of random variables we can generate a graph in which random variables are nodes while the edges
represent dependencies. This graph is called Bayesian Network.
Figure 1 : A simple, graphical notation for conditional independence assertions and hence for compact specification
of full joint distributions.
Machine Learning Notes
! LucasMac!
If we assume that all the hypotheses have the same probability (P(hi ) = P(hj )) we can simplify and choose the
Maximum likelihood (M L) hypothesis:
hM L = argmax
h ∈H
P(D|h)
To generate a M AP hypothesis we can iterate all over the possible hypotheses and for each of this we can compute
the posteriori probability and we take the hypothesis h M AP
with the highest posteriori probability.
Since Bayes theorem provides a principled way to calculate the posterior probability of each hypothesis given the
training data, we can use it as the basis for a straightforward learning algorithm that calculates the probability for
each possible hypothesis, then outputs the most probable. The problem is that we can have infinite hypothesis and
we cannot enumerate all the possibilities. In some case if the space is limited and small it can be computed. So, in
general a learning process can be the brute force:
P(h|D) =
P(D|h)P(h)
h M AP
= argmax
h ∈H
P(h|D) ← Maximum a posteriori
This algorithm may require significant computation, because it applies Bayes theorem to each hypothesis in H to
calculate P(h|D). While this may prove impractical for large hypothesis spaces, the algorithm is still of interest
because it provides a standard against which we may judge the performance of other concept learning algorithms.
Assumptions in order to use Brute Force MAP
The training data D is noise free (i.e., di = c(xi ));
The target concept c is contained in the hypothesis space H;
We have no a priori reason to believe that any hypothesis is more probable than any other.
Given those assumptions the value of P(h) is 1 /|H|.
hM AP : most probable hypothesis given data D but hM AP (x
′ ) may not be the most probable classification cause is
possible that the most probable class has a probability of 0. 4
In general, the most probable classification of the new instance is obtained by combining the predictions of all
hypotheses, weighted by their posterior probabilities.
Consider target function f : X → V, V = {v 1 , ..., vk }, data set D and a new instance x! D:
P(v j
|x, D) =
hi ∈H
P(v j
|x, h i
)P(h i
total probability over H.
P(vj |x, hi ): probability that hi (x) = vj is independent from DD given hi =⇒ P(vj |x, hi ) = P(vj |x, hi, D), where hi does
not depend on x! D =⇒ P(hi |x, D) = P(hi |D).
Bayes Optimal Classifier
Class of a new instance x:
vOB = argmax
vj ∈V
hi ∈H
P(vj |x, hi )P(hi |D)
This is a method in which the value of the classification of an instance x, given a dataset D, is given by the
argmax of the sum of all possible hypothesis, that is sum the probability that each hypothesis give to that
class for that x
′ weighted with the probability of this hypothesis.
Machine Learning Notes
! LucasMac!
5 Bayesian Learning
Example:
P(h 1
|D) = 0. 4 , P(⊖|x, h 1
) = 0 , P(⊕|x, h 1
P(h 2
|D) = 0. 3 , P(⊖|x, h 2
) = 1 , P(⊕|x, h 2
P(h 3
|D) = 0. 3 , P(⊖|x, h 3
) = 1 , P(⊕|x, h 3
therefore (^) "
hi ∈H
P(⊕|x, hi )P(hi |D) = 0. 4
hi ∈H
P(⊖|x, hi )P(hi |D) = 0. 6 , cause h 2 and h 3 give ⊖ and their probability was 0. 3
and
vOB = argmax
vj ∈V
hi ∈H
P(vj |x, hi )P(hi |D) = ⊖
Any system that classifies new instances according to Bayes optimal equation is called a Bayes optimal classifier, or
Bayes optimal learner. No other classification method using the same hypothesis space and same prior knowledge
can outperform this method on average. This method maximizes the probability that the new instance is classified
correctly, given the available data, hypothesis space, and prior probabilities over the hypotheses.
can outperform this method on average. The main problem is that H is big or infinite, so i can not perform
the a posteriori probability.,
vj ∈V
P(v j
|x, D) can correspond to none of the hypotheses in H.
We can’t use Optimal Bayes cause we cannot compute it. The idea now is: I can’t solve the real problem, so under
some assumptions the real problem can be simplified and for this simplified problem I can find the solution. When-
ever I apply some assumption to simplify the problem, we have an approximated solution.
Some Distributions
P(X = 1 ) = θ, P(X = 0 ) = 1 − θ
(e.g. observing head after flipping a coin, extracting a lime candy, ...)
P(X = x; θ) = θ
x ( 1 − θ)
1 −x
variables X 1
n
, each random variable following Bernoully distribution
1
= k 1
n
= k n
; θ 1
, ...., θ n
), with k i
(e.g. observing head after flipping a coin and extracting a lime candy, ...)
Under assumption that random variables X i
are mutually independent, Multi-variate Bernoulli distri-
bution is the product of n Bernoulli distributions:
n (
i= 1
i
= k i
; θ i
P(X = k; n, θ) =
n
k
θ
k ( 1 − θ)
n−k
(e.g., flipping a coint n times and observing k heads, extracting k lime candies after n extractions, ...)
ables with d possible outcomes. Probability distribution of k 1
outcomes for X 1
, ..., k d
outcomes for X d
after n trials (with
i= 1 ...d
k i
= n)
1
= k 1
d
= k d
; n, θ 1
, ...., θ d
n!
k 1
!...k n
θ
k 1
1
· ... · θ
k d
d
Machine Learning Notes
! LucasMac!