Docsity

Appunti corso Machine Learning, Appunti di Machine learning

Toggle sidebar
bg1
Principal Component Analysis
PCA in an unsupervisioned tecnique which aims to reduce the number of fea-
tures of the data keeping as much as possible of the total variance.
It wants to find a subspace P∈Rn×mwhere the columns of Pare orthonormal
so that P P ⊤=Iand form a basis of Rn.
The projection of a point on Pis defined as y=P⊤xand its reconstruction in
the original space is:
x=P y ⇒ˆx=P P ⊤x
We want Pso that it minimizes the reconstruction error:
P= arg min
P
1
N
N
X
i=1
xi−P P ⊤xi
2
It turns out the best Pmatrix is formed by the meigenvectors associated to
the biggest eigenvalues of the covariance matrix of the data.
C=1
KX
i
xix⊤
i=UΣU⊤
Σ =
σ1
σ2
...
σn
with σ1> σ2>· · · > σn
This procedure assumes data are centered, if it’s not the case the first direction
of Pwill be a vector pointing toward the mean of the data cluster.
To address this problem, we subtract from each point the data mean:
C=1
NX
i
(xi−µ)(xi−µ)⊤where µ=1
NX
i
xi
PCA can be interpreted as a linear mapping to a space which preserves only
the directions with highest variance.
If the dimension of the destination space is the same as the original one, it ac-
tually performs a rotation of the data.
mis a hyperparameter and it’s possible to choose a value by cross-validation
or choose it so that its minimum value maintains the total variance above a
threshold.
m= min
mPm
i=1 σi
Pn
i=1 σi≥t
As it’s been already said, PCA doesn’t use class labels, so it can’t be used
to address classification problems. Moreover it’s susceptible to scaling of the
features.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Anteprima parziale del testo

Scarica Appunti corso Machine Learning e più Appunti in PDF di Machine learning solo su Docsity!

Principal Component Analysis

PCA in an unsupervisioned tecnique which aims to reduce the number of fea- tures of the data keeping as much as possible of the total variance. It wants to find a subspace P ∈ Rn×m^ where the columns of P are orthonormal so that P P ⊤^ = I and form a basis of Rn. The projection of a point on P is defined as y = P ⊤x and its reconstruction in the original space is: x = P y ⇒ ˆx = P P ⊤x

We want P so that it minimizes the reconstruction error:

P = arg min P

N

X^ N

i=

xi − P P ⊤xi

2

It turns out the best P matrix is formed by the m eigenvectors associated to the biggest eigenvalues of the covariance matrix of the data.

C =

K

X

i

xix⊤ i = U ΣU ⊤

σ 1 σ 2

.. . σn

with σ 1 > σ 2 > · · · > σn

This procedure assumes data are centered, if it’s not the case the first direction of P will be a vector pointing toward the mean of the data cluster. To address this problem, we subtract from each point the data mean:

C =

N

X

i

(xi − μ)(xi − μ)⊤^ where μ =

N

X

i

xi

PCA can be interpreted as a linear mapping to a space which preserves only the directions with highest variance. If the dimension of the destination space is the same as the original one, it ac- tually performs a rotation of the data. m is a hyperparameter and it’s possible to choose a value by cross-validation or choose it so that its minimum value maintains the total variance above a threshold.

m = min m

 Pm Pi=1^ σi n i=1 σi

≥ t

As it’s been already said, PCA doesn’t use class labels, so it can’t be used to address classification problems. Moreover it’s susceptible to scaling of the features.

Linear Discriminant Analysis

LDA is a supervised learning technique that seeks directions in the feature space that best discriminate between classes. As a byproduct, it can also be used for dimensionality reduction, projecting data into a lower-dimensional space with high class separability. The idea is to find the directions which better allow to separate the classes. A first approach could be to project data onto the direction joining the means of the two classes, but if points of the classes are scattered along that direction, it would not behave differently from PCA, not fully using information about class labels. Then the final goal becomes to maximize the ratio of the covariance between the classes over the covariance within the classes:

L(w) = max w

w⊤SB w w⊤SW w

where:

SB =

N

X^ Nc

i=

ni(μi − μ)(μi − μ)⊤

SW =

N

X^ Nc

i=

X^ ni

j=

(xij − μi)(xij − μi)⊤

Computing the gradient of L(w) and setting it equal to ∅ we get the standard equation for eigenvalues:

∇w

w⊤SB w w⊤SW w

SB w = λSW w where λ =

w⊤SB w w⊤SW w ⇒ λw = S W−^1 SB w

It follows that the optimal vector w is eigenvector of S W−^1 SB with λ an eigen- value.

To derive a classifier from LDA, it assigns a point xt to the class with the closest mean. Imagining that μ 1 is farther than μ 0 :

C(xt) =

C 0 if ∥w⊤xt − μ 0 ∥^2 < ∥w⊤xt − μ 1 ∥^2 C 1 else

¯μ 0 = w⊤μ 0 and μ¯ 1 = w⊤μ 1 (projection of the means on w) (w⊤xt)^2 + ¯μ^20 − 2 w⊤xt ¯μ 0 < (w⊤xt)^2 + ¯μ^21 − 2 w⊤xt μ¯ 1

The goal of this model is to estimate the parameters:

Θ = [(μ 1 ,... , μnc ), (Σ 1 ,... , Σnc )]

which better approximate the class distributions which generated the data. To do it, we define the likelihood function as a measure of how likely to observe x belonging to ci, with parameters Θ:

L(Θ) =

Y^ n

i

f (xi, ci|Θ)

Y^ n

i

f (xi|ci, Θ) · P (ci)

f (xi|ci; Θ) is the class conditional density and P (ci) is the prior, an application dependent parameter.

To avoid numerical issues due to floating point approximation (the function multiplies terms that are probabilities or density of probabilities, so values con- strained between 0 and 1), we take the logarithm of likelihood ℓ(Θ):

ℓ(Θ) =

X

i

log N (xi|μci , Σci ) +

X

i

log P (ci)

X^ C

k=

X

i∈ck

log N (xi|μk, Σk) + ξ

X^ C

k=

ℓk(Θ) + ξ

It turns out we can maximize ℓ(Θ) by maximizing class-independent likelihood, while P (ci) does not depend on Θ.

Taking the gradient of ℓk(Θ) and setting it equal to 0, we get the Maximum Likelihood solutions for μc and Σc:

μ∗ c =

nc

X

i∈c

xi

Σ∗ c =

nc

X

i∈c

(xi − μ∗ c )(xi − μ∗ c )⊤

Once the parameters have been estimated, we can assign a test point to the class which has the highest posterior probability. That one is the ratio of joint density over the marginal density, which is the sum of the joint density over all the classes.

c∗^ = arg max c P (c | x)

For a binary classification problem, we can compute the ratio of the two poste- riors:

r(x) =

P (c 1 |x) P (c 0 |x)

log r(x) = log P (x|c 1 ) P (x|c 0 )

  • log P (c 1 ) P (c 0 )

The first term is the log-likelihood ratio, ℓℓ(x), while the second one is the priors’ log-odds.

Since there are two classes:

P (c 1 ) = π ⇒ P (c 0 ) = 1 − π

ℓℓ(x) > − log

π 1 − π

The priors act as a threshold which moves as the priors change, with the proba- bilistic interpretation that stronger the priors for one class, greater the evidence for the other to let the model ”change idea”.

ℓℓ(x) = log

N (x|μ 1 , Σ 1 ) N (x|μ 0 , Σ 0 )

d 2

log 2π+

log |Σ 0 | − (x − μ 0 )⊤Σ− 0 1 (x − μ 0 )

d 2

log 2π−

log |Σ 1 | − (x − μ 1 )⊤Σ− 1 1 (x − μ 1 )

⇒ ℓℓ(x) = x⊤Ax + b⊤x + c

A = −

(Σ− 1 1 −Σ− 0 1 ) b = Σ− 1 1 μ 1 −Σ− 0 1 μ 0 c = −

μ⊤ 1 Σ− 1 1 μ 1 − μ⊤ 0 Σ− 0 1 μ 0

The decision surface is quadratic in x.

Naive Bayes MVG

Assume no correlation between the features for each class. This forces the covariance matrix to be diagonal:

Σc =

σc, 1 0 · · · 0 0 σc, 2 · · · 0 .. .

0 0 · · · σc,m

The reduction of number of parameters strongly simplifies the computation. In fact, the density of (x|c) can be factorized over its components:

These quantities are derived from the confusion matrix of a binary classifier.

To remove dependency from the class priors, we define error rates conditioned on the class:

Pf n =

F N

F N + T P

, Pf p =

F P

F P + T N

, Ptp =

T P

T P + F N

, Ptn =

T N

T N + F P

The global error rate can be written as a weighted average of class-specific error rates:

error rate =

F N + F P

T P + T N + F P + F N

By adding and subtracting quantities appropriately, we can express this as:

error rate =

F N

F N + T P + T N + F P

F P

F N + T P + T N + F P

Then we reorganize as a weighted sum:

error rate = Pf n · πemp^ + Pf p · (1 − πemp)

Where:

πemp^ =

F N + T P

F N + T P + T N + F P

is the empirical prior of the positive class, i.e., the fraction of positive samples in the dataset. We obtain a weighted sum of class-wise error rates, where the weights are the empirical priors. This allows us to fairly evaluate classifiers on imbalanced datasets, decouple performance metrics from class distributions and better un- derstand the sources of error per class.

Bayes Decision and Bayes Risk

The goal of a classifier R, beyond just assigning labels, is to decide whether or not to take an action a ∈ A, from a set of possible actions, where each action incurs a cost depending on the true class C of the input x. This cost is denoted by:

C(a(x|R) | c)

which represents the cost of taking action a (suggested by recognizer R) when the actual class is c.

The Bayes Risk is the expected cost, i.e., the theoretical mean of the risk:

B =

X^ K

c=

πc

Z

f (x | c)C(a(x|R) | c) dx

where: πc is the prior probability of class c, f (x | c) is the class-conditional density and C(a(x|R) | c) is the cost of the decision given the true class.

In real-world scenarios, we don’t know the true distributions f (x | c), but we have access to labeled samples from which we can approximate the risk empiri- cally.

Let Nc be the number of samples from class c. Then:

Bemp =

X^ K

c=

π cemp ·

Nc

X

i∈c

C(a(xi | R) | c)

where π cemp = N Nc is the empirical class prior.

For the binary case the empirical Bayes risk simplifies to:

Bemp = πCf nPf n + (1 − π)Cf pPf p

This formula makes it explicit how classification errors and cost asymmetries influence the total expected loss.

Cost Detection Function (DCF)

The DCF (Cost Detection Function) represents the mean cost associated with classification decisions. It is also referred to as the unnormalized DCF, and it can be normalized by comparing it with the best dummy system.

A dummy system always predicts one class based on the priors π. Thus:

( Pf n = 1, Pf p = 0 ⇒ DCF = (1 − π) · Cf p (always predicts H 1 ) Pf n = 0, Pf p = 1 ⇒ DCF = π · Cf n (always predicts H 0 )

The normalized DCF is defined as the ratio between the actual DCF and the best dummy DCF:

DCFnorm(π, Cf n, Cf p) =

DCFactual(π, Cf n, Cf p) min (π · Cf n, (1 − π) · Cf p)

Logistic Regression can be motivated starting from the Tied MVG model, where we assume equal covariance matrices for all classes. In this case, the log- likelihood ratio becomes a linear function:

ℓℓr(x) = log

P (H 1 | x) P (H 0 | x)

= ωT^ x + b

Using the fact that P (H 1 | x) + P (H 0 | x) = 1, we derive:

P (H 1 | x) 1 − P (H 1 | x) = eω

T (^) x+b

Solving for P (H 1 | x):

P (H 1 | x) = eω

T (^) x+b

1 + eωT^ x+b^

1 + e−ωT^ x−b^

= σ(ωT^ x + b)

where σ(z) = (^) 1+^1 e−z is the sigmoid function.

The sigmoid function squashes any real-valued input to a value between 0 and 1, making it suitable for modeling probabilities. Logistic regression thus defines a probabilistic decision boundary based on a linear combination of the input features. It also enables a smooth transition between class decisions as a function of input confidence, unlike hard thresholding methods.

We train the model by maximizing the likelihood of the observed labels given the inputs. Assuming independent data points (xi, ci):

L(θ) =

Y

i

P (ci | xi; θ)f (xi)

Taking the log-likelihood and dropping the marginal term f (xi) (which does not depend on θ):

ℓ(θ) =

X

i

log P (ci | xi)

Let yi = σ(ωT^ xi +b), and ci ∈ { 0 , 1 } be the true class label following a Bernoulli distribution. Then: L(ω, b) =

Y

i

yc i i(1 − yi)^1 −ci

ℓ(ω, b) =

X

i

[ci log yi + (1 − ci) log(1 − yi)]

The function to be minimized (negative log-likelihood) is:

J (ω, b) = −ℓ(ω, b) =

X

i

log

1 + e−zi(ω

T (^) xi+b)

where zi = 2ci − 1 ∈ {− 1 , 1 }.

This formulation allows us to interpret the logistic loss as an empirical risk. Even correct predictions are penalized if made with low confidence:

  • zisi > 0: correct prediction, small risk.
  • zisi < 0: wrong prediction, large risk.

where si = ωT^ xi + b. The loss function is:

H(ci, yi) = log(1 + e−zisi^ )

The graph of this loss reveals that, unlike the 0-1 loss which is discontinuous, the logistic loss is smooth and convex. It penalizes highly confident wrong predictions more severely than low-confidence ones.

When the training data is linearly separable, logistic regression has no finite optimum: if (ω, b) separates the data, so does (2ω, 2 b), and so on. The model keeps increasing ∥ω∥ to minimize the loss:

∥ω∥ → ∞, J (ω, b) → 0

This leads to overfitting and poor generalization, especially with small datasets or when decision boundaries are sharp and close to data points.

To counteract this, a regularization term is introduced:

R^ e(ω, b) = λ 2

∥ω∥^2 +

X

i

log

1 + e−˜ci(ω

T (^) xi+b)

Where λ is a hyperparameter that controls model complexity:

  • If λ is too large: underfitting, poor separation.
  • If λ is too small: overfitting, poor generalization.

This regularized version of logistic regression balances data fitting and model simplicity. It generalizes better and avoids degeneracies in separable data. The final model not only provides probabilistic predictions, but also embeds a notion of margin and confidence, making it a foundational tool in modern machine learning pipelines.

Multiclass Logistic Regression

We now extend logistic regression to the multiclass setting. Given K classes labeled 1, 2 ,... , K, the objective is to estimate the posterior class probabilities P (C = k | x) directly, without assuming an underlying generative model for the features.

Quadratic Surfaces and Expanded Feature Spaces

If the class-conditional distributions differ in covariance matrices (e.g., Gaussian classifiers without tied covariance), the posterior log-ratio becomes quadratic:

log P (C = 1 | x) P (C = 0 | x)

= x⊤Ax + b⊤x + c.

To model this discriminatively, we can map the input x to a higher-dimensional feature space using a transformation φ(x):

φ(x) =

vec(xx⊤) x

, w =

vec(A) b

leading to a linear model in the transformed space:

s(x) = w⊤φ(x) + c.

Training proceeds as in standard logistic regression, but now the classifier op- erates in an expanded feature space where quadratic decision boundaries in x become linear in φ(x).

This method is powerful but computationally demanding, as the dimension of φ(x) grows quadratically (or worse) with the input size. It provides a flexible way to approximate more complex decision boundaries without generative as- sumptions.

Experiments on datasets like MNIST show that while standard logistic regres- sion often outperforms simple Gaussian models (due to weak Gaussian assump- tions), the use of quadratic expansions or PCA-preprocessing with Gaussian models can provide superior performance when nonlinear separations are more appropriate.

Support Vector Machines

Support Vector Machines (SVMs) provide an alternative approach to binary classification that offers a geometrical interpretation of regularization. While Logistic Regression maximizes the log-probability of class assignments by mini- mizing the logistic loss, SVMs select the hyperplane that separates classes with the largest margin.

Like Logistic Regression, SVMs can be formulated as a regularized risk mini- mization problem:

min w,b

λ 2

∥w∥^2 +

n

X^ n

i=

ℓ(zi(wT^ xi + b))

where ℓ is the loss function and zi ∈ {− 1 , +1} are the class labels.

However, unlike Logistic Regression, SVM outputs cannot be directly inter- preted as class posteriors. The key advantage of SVMs lies in their natural extension to non-linear separation through the kernel trick, without requiring explicit feature expansion.

Hard Margin SVM

For linearly separable classes, the hard margin SVM selects the hyperplane that maximizes the margin, defined as the distance of the closest point to the separation hyperplane.

Given the separation function f (x) = wT^ x + b, the distance of point xi from the hyperplane is:

d(xi) = |zi(wT^ xi + b)| ∥w∥

The maximum margin hyperplane is found by solving:

w∗, b∗^ = arg max w,b min i∈{ 1 ,...,n}

zi(wT^ xi + b) ∥w∥

Due to the scale invariance of the objective function, we can normalize the parameters such that mini zi(wT^ xi + b) = 1. This transforms the problem into the equivalent constrained optimization:

Primal formulation:

min w,b

∥w∥^2

s.t. zi(wT^ xi + b) ≥ 1 , i = 1,... , n

This is a convex quadratic programming problem. Using Lagrange multipliers αi ≥ 0, the Lagrangian is:

L(w, b, α) =

∥w∥^2 −

X^ n

i=

αi[zi(wT^ xi + b) − 1]

Setting the derivatives with respect to w and b to zero yields:

w =

X^ n

i=

αizixi

X^ n

i=

αizi

By eliminating the slack variables, the primal problem can be rewritten as:

min w,b

∥w∥^2 + C

X^ n

i=

max[0, 1 − zi(wT^ xi + b)]

The function max(0, 1 − s) is called the hinge loss. This formulation is remark- ably similar to regularized Logistic Regression:

SVM: min w,b

λ 2

∥w∥^2 +

n

X^ n

i=

max[0, 1 − zi(wT^ xi + b)]

LR: min w,b

λ 2

∥w∥^2 +

n

X^ n

i=

log[1 + e−zi(w

T (^) xi+b) ]

The key difference lies in the loss function: SVMs use the hinge loss while Logistic Regression uses the logistic loss.

Kernel Methods

The power of SVMs lies in their ability to achieve non-linear separation without explicit feature expansion. Since both training and prediction depend only on dot products, we can define a kernel function:

k(x 1 , x 2 ) = Φ(x 1 )T^ Φ(x 2 )

where Φ is an implicit mapping to a higher-dimensional feature space.

The dual formulation becomes:

Hij = zizj k(xi, xj ) and the prediction function is:

s(xt) =

X

i:αi> 0

αizik(xi, xt) + b

Common kernel functions include:

  • Polynomial kernel: k(x 1 , x 2 ) = (xT 1 x 2 + 1)d
  • RBF kernel: k(x 1 , x 2 ) = e−γ∥x^1 −x^2 ∥

2

This kernel trick enables training in high-dimensional spaces with computa- tional complexity depending only on the number of training samples, not the feature space dimension.

While Logistic Regression can also incorporate kernels, SVMs provide a more natural framework for non-linear classification through the dual formulation, making them particularly effective for complex decision boundaries.

Gaussian Mixture Models

One significant limitation of generative models like Multivariate Gaussian (MVG) is their assumption of a specific distribution that may not accurately represent the data. Gaussian Mixture Models (GMMs) provide an alternative approach to model generic distributions by approximating any sufficiently regular distri- bution to a desired degree of accuracy.

GMMs can be used for various tasks beyond density estimation, including clus- tering as an alternative to K-means. A Gaussian Mixture Model represents a density as a weighted combination of Gaussian components:

X ∼ GM M (M, S, w)

fX (x) =

X^ K

c=

wcN (x; μc, Σc)

where M = [μ 1 ,... , μK ] are the component means, S = [Σ 1 ,... , ΣK ] are the component covariances, and w = [w 1 ,... , wK ] are the mixture weights with PK c=1 wc^ = 1. We have already encountered GMMs in the context of Gaussian classifica- tion, where the marginal distribution is computed as:

fX (x) =

X^ K

c=

πcN (x|μc, Σc)

Hard Cluster Assignments

Given a dataset D = [x 1 ,... , xn] generated independently by a GMM, we can estimate the model parameters θ = [M, S, w] using Maximum Likelihood esti- mation. However, direct ML estimation for GMMs is ill-posed due to potential degenerate solutions with unbounded likelihood.

The likelihood function is:

L(θ) =

Y^ n

i=

fXi (xi) =

Y^ n

i=

X^ K

c=

wcN (xi|μc, Σc)

with corresponding log-likelihood:

ℓ(θ) =

X^ n

i=

log

X^ K

c=

wcN (xi|μc, Σc)

A GMM can be interpreted as the marginal of a joint distribution of data points and corresponding clusters:

fXi (xi) =

X^ K

c=

fXi|Ci (xi|c)P (Ci = c) =

X^ K

c=

wcN (xi; μc, Σc)

where Nc =

P

i γc,i^ is the zero-order statistic and^ Fc^ =^

P

i γc,ixi^ is the first- order statistic.

This represents a weighted empirical mean where the weight of each sample corresponds to its responsibility. Similarly, for the covariance matrix:

Σc =

Nc

X

i

γc,ixixTi − μcμTc =

Sc Nc

− μcμTc

where Sc =

P

i γc,ixix

T i is the second-order statistic.

The weights are re-estimated as:

wc =

Nc N

P

P^ i^ γc,i K c=

P

i γc,i

Since responsibilities depend on parameters and vice versa, we use an iterative procedure: E-step: Estimate responsibilities given current parameters M-step: Re-estimate parameters given current responsibilities

This procedure is a particular instance of the Expectation-Maximization (EM) algorithm.

Model Considerations and Classification

Similar to MVG, we can constrain GMM covariance matrices to be diagonal or tied across components to reduce parameters and computational costs. Proper initialization is critical for GMM performance. Common approaches include:

  • K-means initialization for initial cluster assignments
  • LBG algorithm: iteratively split components starting from a single Gaus- sian

For classification tasks, we fit one GMM per class with Kc components for class c. Class posterior probabilities are computed as:

P (Ct = c|Xt = xt) =

P (Ct = c)

PKc k=1 wc,kN^ (xt|μc,k,^ Σc,k) P c′^ P^ (Ct^ =^ c ′) PKc′ k=1 wc′,kN^ (xt|μc′,k,^ Σc′,k)

For binary tasks, we can use the log-likelihood ratio:

llr(xt) = log

PK 1

P^ k=1^ w^1 ,kN^ (xt|μ^1 ,k,^ Σ^1 ,k) K 0 k=1 w^0 ,kN^ (xt|μ^0 ,k,^ Σ^0 ,k)

GMMs are particularly useful for open-set multiclass classification, where the ”none-of-others” class can be modeled as a GMM using unlabeled samples, providing flexibility in handling heterogeneous reject classes.