































































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!
































































Course taught by Prof.
Based on the notes of
Politecnico di Torino MSc in Computer Engineering - Artificial Intelligence and Data Analytics
Course ID: 01URTOV A.Y. 2024-
Chapter 1. Dimensionality Reduction 5
In this chapter, we analyze techniques for dimensionality reduction, a process whose goal is to compute a mapping from an initial n-dimensional feature space to an m-dimensional one (with m ≪ n), preserving most of the useful informa- tion. This comes in handy in various scenarios, such as compressing information, removing noise, reducing the risk of overfitting, and enabling data visualization. Different objectives may call for different approaches. To achieve the best possible outcome, we can choose between unsupervised and supervised meth- ods. The former seeks to uncover underlying structures in the data without any feedback (labels), whereas the latter relies on both input and output data and aims to estimate a mapping between them.
We start with an unsupervised approach, whose goal is to map the data into a lower-dimensional feature space while preserving as much information as possible. Since this method is unsupervised, we do not take classes or labels into account, but only the samples themselves. The main objective is to find an optimal matrix P ∈ Rn×m^ with orthonormal columns, that defines our subspace.
To achieve this, we need a criterion to evaluate the quality of a solution. A common choice is the minimization of the average reconstruction error:
J(P ) = argmin(
i=
∥xi − xˆi∥^2 ),
where N is the number of samples in the dataset and ˆxi is the reconstruc- tion of xi from the reduced subspace back into the original space. In simple terms, we aim to find the set of vectors that best allow us to reconstruct the original samples from their lower-dimensional representations, while losing as little information as possible.
Chapter 1. Dimensionality Reduction 7
will be connecting to the origin and dataset mean. In most cases, this direction is not very interesting, so the solution to this problem is to remove the mean of the data set to the samples before applying PCA.
Figure 1.1: PCA with not zero-mean dataset
If ¯x is the mean of the data set, the subspace is computed by maximizing:
J(P ) = Tr P T
i=
(xi − ¯x)(xi − x¯)T
The matrix inside the brackets is called the covariance matrix, which measures how the variables in the dataset vary together. Intuitively, this matrix contains information about the direction and strength of linear relationships among features.
Projecting our centered data samples onto the matrix P T^ corresponds to expressing the data in a new coordinate system defined by the columns of P. This changes the basis of the data space to directions where the components become uncorrelated.
This happens because the covariance matrix C is symmetric and can be diagonalized as C = U ΣU T^ , where U contains the eigenvectors and Σ is a diagonal matrix of eigenvalues. When we project data onto these eigenvectors (i.e., U T^ x), the covariance matrix of the transformed data is Σ, which has zeros in all off-diagonal elements.
Geometrically, this can be seen as a rotation of the original data cloud so that its major axes align with the coordinate directions. In the original space, correlations appear as an oblique orientation of the distribution; after projection, these axes are orthogonal and independent in terms of covariance.
Figure 1.2: Projection of data onto PCA directions aligns the data with uncor- related axes
Moreover, the eigenvalues in Σ correspond to the variance of the data along each new axis (each principal component). Specifically, the variance along eigen- vector p is given by its associated eigenvalue λ. Let u be an eigenvector of the covariance matrix C, and let λ be the corre- sponding eigenvalue. By definition:
Cu = λu
The variance of the data projected onto the direction u is given by:
Var(x⊤u) = u⊤Cu (1.2)
Because u is an eigenvector of C, we have:
u⊤Cu = u⊤(λu) = λ(u⊤u) = λ
assuming ui is a unit vector. This also explains why maximizing J(P ) = Tr
is equivalent to se- lecting eigenvectors associated with the largest eigenvalues, which capture the most variance in the data. This ensures that by projecting onto the first m eigenvectors (principal components), we retain the maximum amount of infor- mation (variance) while reducing dimensionality. In summary, PCA finds a new coordinate system where the features are uncorrelated and ordered by the amount of variance they capture. This trans- formation helps in simplifying the data structure and improving interpretability without losing significant information.
Given its unsupervised nature, Principal Component Analysis (PCA) has lim- itations when it comes to finding discriminant directions. In many scenarios, the data is heterogeneous and must be scaled, which can significantly affect the outcome of PCA. Most importantly, PCA does not incorporate class label infor- mation; it searches for directions that maximize overall variance, regardless of any underlying class structure. Therefore, if our goal is to analyze or separate
The direction of projection w is a vector in Rn^ (w ∈ Rn). Geometrically, the objective function can be interpreted as follows: the numerator wT^ SB w repre- sents the spread of the class means along the direction w, while the denominator wT^ SW w represents the overall spread of the samples within each class along the same direction. Maximizing J(w) finds the directions where projected class means are max- imally separated relative to the class scatter (compactness) projected in those directions. It can also be shown that
c=
X^ nc
i=
(xc,i − μ)(xc,i − μ)T^ ,
which is the covariance matrix of the whole dataset. The optimal solutions are found by solving the generalized eigenvalue prob- lem: S W−^1 SB w = λw,
where w ∈ Rn^ is the eigenvector and λ the corresponding eigenvalue. As well as dimensionality reduction, LDA can be used as a classification model. To derive a classification rule (we first look at bonary problems), we consider a simple classifier based on distance of a sample from the class means in the projected space:
C(xt) =
C 1 if wT^ xt − wT^ μ 1 2 < wT^ xt − wT^ μ 2 2
C 2 otherwise
Let ¯μ 1 = wT^ μ 1 and ¯μ 2 = wT^ μ 2. A data point xt is assigned label C 1 if
wT^ xt − μ¯ 1
2 < wT^ xt − μ¯ 2
2
Expanding and simplifying, this condition becomes:
(wT^ xt − μ¯ 1 )^2 < (wT^ xt − μ¯ 2 )^2
⇐⇒ (wT^ xt)^2 + ¯μ^21 − 2 wT^ xt μ¯ 1 < (wT^ xt)^2 + ¯μ^22 − 2 wT^ xt μ¯ 2 ⇐⇒ μ¯^21 − ¯μ^22 + 2wT^ xt(¯μ 2 − ¯μ 1 ) < 0
Chapter 1. Dimensionality Reduction 11
⇐⇒ (2wT^ xt − μ¯ 1 − μ¯ 2 )(¯μ 2 − ¯μ 1 ) < 0
Assuming we choose w so that ¯μ 2 − μ¯ 1 > 0 (the orientation is such that the projected mean of class 2 is larger than that of class 1), the decision rule becomes:
C(xt) =
C 1 if wT^ xt < t C 2 if wT^ xt ≥ t
where
t = ¯μ 1 + ¯μ 2 2 In essence, we project the data onto the direction connecting the centroids, and use t as a threshold (the mean of the projected centroids): in a binary classification problem, if the projected value of xt is above t, the sample is assigned to one class; if it is below t, to the other. Until now, we have focused on the binary classification problem. We can extend the analysis to multiclass problems. In terms of dimensionality reduction, instead of projecting our data onto a single vector w, the projection becomes a matrix W ∈ Rn×m, where m ≤ C − 1 and C is the number of classes. The projected between-class and within-class covariance matrices become:
S^ cB = W T^ SB W,
S^ dW = W T^ SW W,
where SB , SW ∈ Rn×n^ are the original scatter matrices. Since we are no longer restricted to a single projection vector, we cannot talk about the ratio of two scalars as in the binary case. Instead, a common criterion is to maximize the trace of the matrix product:
J(W ) = Tr(SdW
− (^1) c SB ), (1.4)
which generalizes the ratio used in the two-class problem by considering the sum of the variances along the top m discriminant directions. The optimal W contains the m eigenvectors of S W−^1 SB corresponding to the largest eigenvalues. Notice that from the definition of SB , the number of non-zero eigenvalues is at most C − 1. This happens because the between-class scatter matrix is constructed from the C class means {μ 1 , μ 2 ,... , μC }, which lie in an affine subspace of dimension at most C − 1 (for example, two points lie on a line, three points on a plane, etc.). As a consequence, SB has rank ≤ C − 1. Therefore, Linear Discriminant Analysis can estimate at most C −1 discrim- inant directions which are useful for class separation. In summary:
Chapter 2. Generative Gaussian Models 13
Now we will focus on Gaussian classifiers, a set of generative models that as- sume a normal distribution for each class in the dataset. By estimating model parameters, these classifiers fit the best possible Gaussian curve to the data, allowing for optimal classification decisions. In particular, we will discuss three different models, each with different assumptions. These models work with the multivariate Gaussian distribution, which is simply a generalization of the standard (1-dimensional) normal distribution to multiple features. Instead of modeling just one feature at a time, the multi- variate Gaussian can describe the behavior and relationships of several variables together, using a mean vector (that indicates the average value for each feature) and a covariance matrix (that captures how the features vary and relate to each other). This makes it well-suited for handling datasets where features may be correlated or interact in complex ways. For example, consider a dataset with two features: height and weight. If we model these using the multivariate Gaussian distribution, the mean vector would contain the average height and weight, while the covariance matrix would encode how the two features vary together (for instance, if taller people also tend to weigh more, the covariance would be positive). Using the multivariate Gaussian allows us to capture and classify not just the typical values for each feature, but also the way in which feature values change together for each class.
The first model we analyze is the multivariate Gaussian (MVG) classifier, a generative model that assumes the data are distributed according to class- dependent Gaussian distributions. Specifically, the following assumptions are
14 2.1. Multivariate Gaussian Classifier
made:
(X | C = c) ∼ N (μc, Σc).
[(Xi, Ci) ⊥⊥ (Xj , Cj )] | θ, and (Xi, Ci) | θ ∼ (X, C) | θ.
If the parameters μc and Σc were known, we could directly compute the like- lihood of each class. Since they are unknown, we must estimate them from data. We denote by θ = {μc, Σc}kc=1 the collection of model parameters. The likelihood of the parameters θ given the training set is the joint density of all the observed variables:
L(θ) = fX 1 ,...,Xn, C 1 ,...,Cn|θ (x 1 ,... , xn, c 1 ,... , cn | θ).
By independence,
L(θ) =
Y^ n
i=
fX,C|θ (xi, ci | θ).
Decomposing each term using Bayes’ rule,
L(θ) =
Y^ n
i=
fX|C,θ (xi | ci, θ) P (ci).
Since the class-conditional densities are Gaussian, we obtain
L(θ) =
Y^ n
i=
N (xi | μci , Σci ) P (ci).
16 2.1. Multivariate Gaussian Classifier
Figure 2.1: MVG with 2 class problem and 2-features (correlated) samples
All the models we will cover can be applied to classification tasks. We distin- guish between binary classification and multiclass classification. Since the binary case is often simpler to analyze, we start with two classes. In this setting, it is useful to consider the ratio between the two posterior probabilities. For convenience, we express it in logarithmic form:
log r(xt) = log
P (C = h 1 | xt) P (C = h 0 | xt)
This expression can be rewritten to make explicit its dependency on the likelihoods and class priors:
log r(xt) = log
P (C = h 1 | xt) P (C = h 0 | xt)
= log fX,C (xt, h 1 ) fX,C (xt, h 0 )
= log
fX|C (xt | h 1 ) P (C = h 1 ) fX|C (xt | h 0 ) P (C = h 0 )
= log
fX|C (xt | h 1 ) fX|C (xt | h 0 )
The first term is called the log-likelihood ratio (LLR). It quantifies the degree of support the data xt provides in favor of one class versus the other. Formally, the LLR compares how likely it is to observe xt under class h 1 relative to class h 0 :
llr(xt) = log
fX|C (xt | h 1 ) fX|C (xt | h 0 )
Chapter 2. Generative Gaussian Models 17
If llr(xt) is positive, the observation xt is more likely under class h 1 than under h 0 , so the system favors assigning h 1. If llr(xt) is negative, xt supports class h 0 instead. Intuitively, the LLR acts as a score: higher values provide evidence for class h 1 , lower values favor class h 0. This score-driven approach has a probabilistic interpretation—by thresholding the LLR, the classifier can assign a sample to the class for which it finds more evidence in the data.
The second term is the prior log-odds, which measures how much more likely one class is compared to the other before observing any data. In other words, it represents the initial bias of the model in favor of one class over the other, determined only by the prior probabilities. For a binary problem, let
P (C = h 1 ) = π, P (C = h 0 ) = 1 − π.
Then
log r(xt) = log
fX|C (xt | h 1 ) fX|C (xt | h 0 )
π 1 − π
The optimal decision rule is:
log r(xt) ⋛ 0 ,
that is,
log
fX|C (xt | h 1 ) fX|C (xt | h 0 )
π 1 − π
Equivalently, we can compare the log-likelihood ratio directly to the prior log-odds:
llr(xt) = log
fX|C (xt | h 1 ) fX|C (xt | h 0 )
⋛ − log
π 1 − π
In particular, we assign class h 1 if
llr(xt) > − log
π 1 − π
and class h 0 otherwise. Since we are comparing log-likelihood ratio with a treshold, we can come to the conclusion that the decision function is quadratic in x:
llr(x) = xT^ Ax + xT^ b + c
this leads to elliptical or hyperbolic decision boundaries. For example, we can obtain something like:
Chapter 2. Generative Gaussian Models 19
For multiclass problems C ∈ {h 1 , h 2 ,... , hk}, we compute the posterior probabilities for each class as
P (C = hi | xt) =
fX|C (xt | hi) P (hi) P h′∈{h 1 ,...,hk } fX|C^ (xt^ |^ h ′) P (h′)
Optimal classification selects the class with the highest posterior probability:
c∗ t = arg max h P (C = h | xt)
Since the denominator is the same for all classes, it is sufficient to maximize the numerator. Posterior probabilities are proportional to
P (C = hi | xt) ∝ fX|C (xt | hi) P (hi)
Thus, the optimal decision rule can be computed as
c∗ t = arg max h log fX|C (xt | h) + log P (h) (2.5)
Here, log fX|C (xt | h) is the score given by the model for class h, while log P (h) encodes the class prior and may depend on the application.
As we have seen, the MVG model requires computing a mean vector and a covariance matrix for each class. This can be problematic when the dataset has few samples compared to its dimensionality, often leading to inaccurate parameter estimates. The issue is particularly severe for covariance matrices: if each sample is x ∈ RD^ (where D is the number of features), then estimating the covariance matrix requires computing D(D 2 +1) parameters (since the matrix is symmetric). If D is large and the dataset is small, this estimation becomes unreliable. The Naive Bayes classifier addresses this issue by making different assump- tions compared to the MVG model.
The model is based on the following assumptions (the first three are the same as for MVG):
where xi ∈ Rd^ are the observed samples and ci ∈ { 1 ,... , k} are the corresponding class labels.
20 2.2. Naive Bayes Classifier
(X | C = c) ∼ N (μc, Σc).
[(Xi, Ci) ⊥⊥ (Xj , Cj )] | θ, and (Xi, Ci) | θ ∼ (X, C) | θ.
fX|C (x | c) ≈
j=
fX[j]|C (x[j] | c),
where xj is the j-th feature of the sample x. This means that the class- conditional density of a sample can be expressed as the product of the class-conditional densities of its features.
Since we are considering each feature independently from the others, we can model the distribution of each component as a univariate Gaussian. In other words, each feature of a sample is treated as if it were the only variable:
fX[j]|C (x[j]|c) = N
x[j] | μc,[j], σ^2 c,[j]
The likelihood for the model parameters then factorizes over all data points and all features:
L(θ) ∝
Y^ n
i=
j=
xi,[j] | μci,[j], σ c^2 i,[j]
Switching to the log-likelihood, the product becomes a sum:
ℓ(θ) = ξ +
X^ k
c=
i|ci=c
j=
log N
xi,[j] | μc,[j], σ c,^2 [j]
= ξ +
j=
X^ k
c=
i| ci=c
log N
xi,[j] | μc,[j]], σ^2 c,[j]