Data Analysis - Supervised Learning
PCA
使用SVD分解和使用协方差矩阵进行计算的一致性。
In the classical implementation of PCA, we explicitly compute the covariance matrix of the data. However, in this task, PCA method is based on SVD, which avoids the need to explicitly compute the covariance matrix.
Proof:
is the data matrix, where is the number of features and is the number of samples. The covariance matrix can be computed as follows: We perform the singular value decomposition of
: Where
is the left singular vector matrix, is the diagonal matrix of singular values, and is the right singular vector matrix. Derivation:
Expand C using the SVD of X:
where we used the fact that
, the identity matrix. Now, observe that:
is a diagonal matrix with the squares of the singular values on the diagonal. - Therefore,
can be expressed as:
where,
- The columns of U are the eigenvectors of C
- The eigenvalues are the scaled squared singular values
, which correspond to the varience captured along each principal component.
Therefore, SVD-based PCA is mathematically equivalent to the classical PCA approach via covariance matrix eigen-decomposition.
Note:
should be Hereafter, we will use the covariance matrix as
for the PCA implementation. Kernel PCA的详细推导过程。
Kernel PCA is a combined technique of PCA and the kernel trick, where we are still interested in using the PCA process to find the features
. However, such a transformation from to now becomes non-linear, as a non-linear kernel function can be applied to first transformed to in a superspace with , then, the linear PCA is performed to transform to . This kernel PCA process brings a major advantage: - Since the calculation of
can be non-linear, and the dimensionality of is now with , these characteristics allow us to search for solutions in a new space (not limited by the original dimentionality ), and such solutions may be linear.
For example, with kernel PCA, for a linearly-inseparable dataset
with a low dimensionality, e.g., d = 2, now it may be possible to solve such classification task with linear solutions, while in a new space. However, we would like to avoid the explicit computation of the high-dimensional
for the PCA decomposition, which can be done by involving the kernel function
with the plain PCA, creating the kernel PCA solution. Two different kernel function will be explored here: - Homogeneous Polynomial kernel:
, where is the polynomial degree. - Radial Basis Function (RBF) kernel:
, where and is the width or scale of a Gaussian distribution centered at .
Mathematically prove howwe can compute the PC Martix?
First things first, we denote:
is the input matrix, where and are the number of the features and samples, respectively. is the -th column vector for . Therefore, . is a nonlinear transformation. . is the mapped matrix on a higher or infinity dimensional eigenspeace . is the -th column vector for . Therefore, . is the Gram matrix, whose element is given by the kernel function
The first thing is to center the mapped matrix
in the feature space , which is defined as follows: where
is the vector of ones. Then, centered mapped matrix
can be denoted as: Similar to the Linear PCA, we have (SVD)
where:
is the left singular vector (orthonormal) matrix, whose column vectors are eigenvectors of , is the diagonal matrix of singular values, whose elements are ordered from largest to smallest, i.e., , and is the right singular vector (orthonormal) matrix, whose column vectors are eigenvectors of .
Notice that the covariance matrix
of and the Gram matrix of are denoted as: and both
and are symmetric matrices. Therefore, it is clear that:
and
If
is centered, then and are both centered. Therefore, it is no need to explicitly centered or . The centered Gram matrix can be computed as follows: If we denote
, which is a matrix, all the elements of are equal to , then, we have: From now, we use
to refer centere Gram matrix . We then still follow the method of finding the first principal component. We know that the PCs are the eigenvectors of
. Notice that the column vectors of are eigenvectors of , therefore, we have: Since the eigenvectors
is a linear combination of , which is where the
is the linear combination factor. We may contrict the mangitude of is equal to 1, i.e., . Therefore,
which means that the eigenvalues of
are , and the eigenvectors of are . Therefore, from the eigen value decomposition, we can derive that
thus, we know that
which is
, and which is
. Therefore, after normalize to obtain a unit vector, we have: Therefore, the final Principal Components are given by:
where
is the number of the PCs. Mathematically prove how to compute the transformed dataset.
From the subtask 1, we obtain the PC matrix
. We can then compute the transformed dataset as follows: Expanding
, we have: For
in , where , and , we have where
is the -th column of the Gram matrix . Therefore,
where,
-
is the diagonal matrix, comprised of , the eigenvalues of . -
is the diagonal matrix, comprised of , the inverse of the square root of , where it refers to the top k eigenvalues of . -
is the matrix, comprised of the top k eigenvectors of . -
is the Gram matrix, where , which is the kernel function. Therefore, to obtain the tranformed dataset
, we need to compute the Gram matrix first and center it, then, we use a eigen value decomposition to obtain the and , and finally, using the above equation, we can compute the transformed dataset using the above equation. However, since the direction of the optimization is the same, we sometimes can remove the
term. This is how the KernelPCAin the PackageScikit-learnworks. Advantages are:- Improved Numerical Stability: Omitting the factor prevents transformed coordinates from becoming extremely small, especially for large n. This avoids potential floating-point precision issues, underflow errors, and increased sensitivity to rounding errors in subsequent computations on the reduced-dimensional data.
- Direct Kernel Space Scaling: This scaling is arguably more natural within the kernel context and avoids an arbitrary dependency on n without losing the essential relative geometry between data points.
- Formula Conciseness: The transformation formula is simpler and more directly linked to the kernel matrix eigendecomposition.
- Since the calculation of
K-Means
在K-Means的收敛性推导中,After the updating step, the sum of squared distance is also ensured to not increasing (≤), if Euclidean distance is used to measure data similarity. 解释一下原因。
在K-means算法中,更新步骤后(即重新计算簇中心后),平方距离之和(Sum of Squared Distances, SSD)确保不会增加(
)。这主要是因为K-means算法的本质是一个迭代优化过程,它在每一步都试图最小化SSD。 具体来说,当使用欧几里得距离作为相似性度量时,每次迭代分为两步:
- 分配步(Assignment Step)
在这一步中,每个数据点
被分配到离它最近的簇中心 。这个操作本身就是为了最小化每个数据点到其所属簇中心的距离平方。因此,在分配步之后,SSD必然会减少或保持不变。因为如果一个数据点可以分配到一个更近的簇中心,那么将其重新分配到那个更近的簇中心必然会降低总体的SSD。 - 更新步(Update Step)
在这一步中,每个簇的中心被更新为其内部所有数据点的均值。数学上可以证明,对于一个给定的数据点集合,这些点的均值是使集合内所有点到该均值的平方距离之和最小的那个点。
假设一个簇
包含数据点 。我们想找到一个点 来最小化 。对 求导并令其为零,可以得到 ,即这些点的均值。这意味着,在更新簇中心后,每个簇内部的平方距离之和达到了局部最小。 综合以上两步,每次迭代都会使得总的平方距离之和减小或者保持不变。
- 分配步确保了每个点到其当前所属簇中心的距离是最小的(对于给定的簇中心)。
- 更新步确保了每个簇的中心是其内部数据点的最优代表(使得簇内平方距离最小)。
这两步的联合作用保证了SSD在一个非递增的序列中。由于SSD是非负的,并且每次迭代都会减小或不变,这个过程最终会收敛到一个局部最优解,即SSD不再发生显著变化。
K-Means++.
K-Means++ 是一种用于优化 K-Means 聚类算法初始质心选择的方法。
标准的 K-Means 算法有一个显著的缺点:它的聚类结果和收敛速度对初始簇中心(也称为质心或均值点)的选择非常敏感。
- 如果初始质心选择得不好(例如,所有初始质心都挤在数据点的某一小部分区域),K-Means 算法很容易陷入局部最优解,导致最终的聚类效果很差,无法准确地反映数据的真实分布。
- 糟糕的初始选择还会导致算法需要更多的迭代才能收敛,从而降低效率。
K-Means++ 就是为了解决这个问题而提出的。它的目标是选择一组“更好”的初始质心,使得这些质心在数据空间中尽可能地分散开来,从而提高 K-Means 算法的收敛速度和聚类质量。
K-Means++ 的核心思想是:让选择的下一个初始质心,尽可能地远离已经选择的质心。 这样可以确保选出的质心能够更好地覆盖整个数据空间,减少初始质心集中于某一区域的可能性。
假设我们要将数据集聚类成
个簇。K-Means++ 选择 个初始质心的步骤如下: 选择第一个质心:
- 从所有数据点中随机均匀地选择一个点作为第一个簇中心
。
- 从所有数据点中随机均匀地选择一个点作为第一个簇中心
选择后续质心(核心步骤):
- 对于数据集中的每一个数据点
,计算它到目前为止已经选择的所有簇中心中最近那个中心的距离。我们将这个距离表示为 。例如,如果已经选择了 个质心 ,那么 (通常使用平方欧几里得距离)。 - 接下来,选择下一个簇中心时,不再是随机均匀地选择,而是采用带权重的随机抽样。点
被选为下一个质心 的概率与其 成正比。 具体公式为: 这意味着,距离当前已选质心越远的点,被选为下一个质心的概率就越大。
- 对于数据集中的每一个数据点
重复步骤 2:
- 重复执行步骤 2,直到我们成功选择了
个簇中心。
- 重复执行步骤 2,直到我们成功选择了
运行标准 K-Means:
- 一旦
个初始质心被选择出来,就将它们作为标准 K-Means 算法的初始点,并按照 K-Means 的迭代过程(分配步和更新步)继续进行聚类,直到收敛。
- 一旦
K-Means++ 的优点
- 提高聚类质量: 通过更合理地初始化质心,K-Means++ 显著降低了 K-Means 算法陷入局部最优解的风险,从而得到更优的聚类结果。
- 加快收敛速度: 更好的初始质心通常意味着算法能够更快地找到稳定的聚类结果,减少迭代次数。
- 简单易实现: 尽管比随机初始化复杂一些,但 K-Means++ 的逻辑相对直观,易于实现。
K-Means++ 是一种改进的 K-Means 算法初始化策略。它通过一种“距离最远优先”的随机抽样方法来选择初始簇中心,确保这些中心在数据空间中尽可能分散,从而有效提升了 K-Means 算法的聚类性能和稳定性。
Soft K-means.
在探讨 Soft K-means 算法时,其核心在于如何处理数据点对簇的归属这一不确定性。与传统的 K-means 算法中每个数据点被“硬性”地唯一分配给一个簇不同,Soft K-means 引入了概率的概念,允许每个数据点以一定的“责任”(responsibility)或概率属于多个簇。这种不确定性,使得数据点所属的簇成为了一个隐藏变量(Latent Variable)。
公式:
- 隐藏变量的引入
在 Soft K-means 中,我们无法直接观测到每个数据点究竟属于哪个簇。例如,一个数据点可能位于两个簇的中间区域,此时对其进行硬性划分会丢失信息。因此,将数据点
属于哪个簇这一信息视为一个隐藏变量 是必要的。我们的目标是估计这个隐藏变量的分布,以及模型的其他参数(如簇中心、簇的权重和方差等)。
- EM 算法在 Soft K-means 中的应用
为了解决含有隐藏变量的概率模型的参数估计问题,期望最大化(Expectation-Maximization, EM)算法成为了 Soft K-means 的核心优化方法。EM 算法通过迭代的两个步骤,间接地最大化观测数据的似然函数:
E 步(期望步 - Expectation Step): 在此步骤中,我们利用当前已知的模型参数(例如,上一步迭代得到的簇中心和方差),来计算每个数据点
属于每个簇 的后验概率。这个后验概率就是所谓的“责任”或“软分配概率”,通常表示为 或 。它量化了在当前模型参数下,数据点 属于簇 的“置信度”。这个步骤是对隐藏变量 的期望进行估计,为后续的 M 步提供依据。 M 步(最大化步 - Maximization Step): 在 E 步计算出每个数据点对每个簇的责任后,M 步的目标是更新模型参数,以最大化当前观测数据在这些“责任”下的似然函数。例如,新的簇中心是通过所有数据点的加权平均计算得出的,其中权重即为该点对该簇的责任。这个过程本质上是在寻找一组新的模型参数,使得在给定 E 步的隐藏变量估计下,观测数据的概率达到最大。
- 最大似然估计(MLE)思想的体现
整个 Soft K-means 算法,通过 EM 迭代过程,完美地体现了**最大似然估计(Maximum Likelihood Estimation, MLE)**的思想。
EM 算法的目标是最大化观测数据
的边际似然 ,其中 代表所有模型参数。由于存在隐藏变量 ,直接最大化边际似然是困难的。EM 算法通过转而最大化期望的完全数据对数似然 来实现这一目标。
- E 步负责计算这个期望值,即根据当前参数和观测数据,得出隐藏变量的最佳后验分布(即责任)。
- M 步则在此期望值的基础上,选择新的参数来最大化它。
每一次 EM 迭代都保证似然函数是非递减的,从而确保算法最终收敛到一个局部最优解。因此,Soft K-means 利用 EM 算法来处理不确定的簇分配这一隐藏变量,正是对最大似然估计原理的实际应用和实现。
DBSCAN
- DBSCAN is short for Density-Based Spatial Clustering of Applications with Noise.
DBSCAN (Density-Based Spatial Clustering of Applications with Noise) 是一种基于密度的聚类算法。与 K-Means 等需要预先指定聚类数量(K值)的算法不同,DBSCAN 能够发现任意形状的簇,并且能够识别出噪声点(异常点)。
DBSCAN 的核心思想是:一个簇是由密度相连(density-reachable)的点的最大集合。它认为,如果一个区域的点密度足够高,那么这些点就属于同一个簇。
DBSCAN 算法主要依赖于两个核心参数:
(epsilon) / eps:
- 半径。它定义了一个圆形邻域的半径。对于数据集中的每个点,DBSCAN 会考虑在这个半径内有多少个其他点。
MinPts:
- 最小点数。在一个点的
半径邻域内,如果包含的点的数量达到或超过 MinPts,那么这个点就被认为是核心点(Core Point)。 根据这两个参数,DBSCAN 将数据点分为三种类型:
核心点(Core Point):
- 如果一个点在其
邻域内包含至少 MinPts 个其他点(包括它自己),那么它就是一个核心点。核心点是簇的“骨架”。 边界点(Border Point):
- 如果一个点在其
邻域内包含的点数少于 MinPts,但它位于某个核心点的 邻域内(即它是某个核心点的直接密度可达点),那么它就是一个边界点。边界点是簇的“边缘”。 噪声点(Noise Point / Outlier):
- 如果一个点既不是核心点,也不是边界点(即它在其
邻域内包含的点数少于 MinPts,并且它不属于任何核心点的邻域),那么它就是一个噪声点。噪声点不属于任何簇。 DBSCAN 算法的聚类过程大致如下:
- 从数据集中随机选择一个未被访问的点。
- 检查这个点的
邻域。
- 如果其邻域内的点数小于 MinPts,则将该点标记为噪声点(暂时,它之后可能成为边界点)。
- 如果其邻域内的点数达到或超过 MinPts,则将该点标记为核心点,并创建一个新的簇。
- 将该核心点邻域内的所有点添加到当前簇中。对于这些新加入的点,如果它们也是核心点,则递归地扩展它们的邻域,将更多密度相连的点加入到当前簇中。
- 重复上述过程,直到所有点都被访问并标记(属于某个簇或被识别为噪声)。
优点:
- 无需预设簇的数量 K:DBSCAN 能够根据数据的密度自动发现簇的数量。
- 发现任意形状的簇:它不像 K-Means 那样只擅长发现球状簇,DBSCAN 可以识别出不规则形状的簇,如L形、S形等。
- 识别噪声点:DBSCAN 能够明确地区分出哪些点是噪声,不将它们强制分配给任何簇。
- 对初始点不敏感:聚类结果对起始点的选择不敏感(除非起始点本身是噪声点)。
缺点:
- 参数选择困难:
和 MinPts 这两个参数的选取对聚类结果有很大影响,且通常需要人工经验或多次尝试。 - 处理密度差异大的簇有困难:如果数据集中存在密度差异很大的簇(例如,一个非常密集的簇和一个非常稀疏的簇),DBSCAN 很难用同一组参数同时很好地处理它们。
- 高维数据表现不佳:在高维空间中,距离度量的有效性降低,选择合适的
变得更加困难,导致“维度灾难”问题。 总而言之,DBSCAN 是一种强大且灵活的聚类算法,特别适用于处理具有复杂形状簇和包含噪声的数据集。
MLE to MAP
Formula.
For Maximum Likelihood Estimation:
MLE就是求一个参数的取值,使得这个参数在这个样本上面表现的最好。
For Maximum A Posteriori:
MAP是已经给定了这个数据,需要结合先验知识来得到一个后验的估计。这个后验的估计要求既能够较好地反映这个数据的分布特征,又要使这个参数在一般的常理之内。
只是Bayes Theorem.
GMM
- 在高斯混合模型中,如果我的所有数据都只用一个多元高斯分布来进行刻画,EM算法还有没有使用的必要?
EM算法的目的: EM算法(期望最大化算法)主要用于含有隐变量的概率模型的参数估计。在高斯混合模型中,隐变量是每个数据点所属的高斯分量。当你有多个高斯分量时,你需要EM算法来迭代地估计每个数据点属于哪个分量(E步),然后根据这个估计来更新每个分量的参数(M步)。
单个高斯分布的参数估计: 如果您的模型只有一个多元高斯分布,那么就没有“混合”的概念,也没有隐变量来指示数据点属于哪个高斯分量(因为它只有一个)。在这种情况下,您可以直接使用**最大似然估计(MLE)**来求解该多元高斯分布的参数:
- 均值(Mean):所有数据点的样本均值。
- 协方差矩阵(Covariance Matrix):所有数据点的样本协方差矩阵。
这些参数可以直接通过封闭形式的解计算出来,不需要迭代过程。
总结:
- 高斯混合模型(GMM):当有多个高斯分量时,数据点的所属分量是隐变量,需要EM算法来估计参数。
- 单个多元高斯分布:没有隐变量,可以直接通过最大似然估计(计算样本均值和样本协方差)来估计参数,无需EM算法。
- 为什么GMM算法没有闭式解?
There is NO closed-form solution for them, one obvious reason is the interdependence of
with and . We need another solution to compute , under the perspective of maximizing data log-likelihood The Expectation-Maximization algorithm introduces a way to address this task.
It is very similar to the two-step process in k-means
当我们在 GMM 中最大化似然函数时,我们会遇到一个对数内部包含求和项的表达式:
这个对数内部的求和项正是症结所在。它意味着我们不知道每个数据点
究竟是由哪个高斯分量生成的(这就是隐变量)。 如果这个隐变量已知,我们就能将问题分解成多个独立的高斯分布估计,每个都有闭式解。 然而,由于隐变量是未知的,我们无法直接求解对数似然函数的导数并将其设为零来得到解析解。 EM 算法正是为了解决这类含有隐变量的问题而生。它通过迭代的方式,先“猜测”隐变量的分布(E 步),然后基于这个猜测更新模型参数(M 步),从而逐步逼近最优解。
模型(GMM)的 E 步中,为什么对于每一个数据点
,其对所有高斯分量 的责任 之和 必须等于 1?这个性质与 M 步中混合权重 的更新有什么关系? - 为什么
?
这个等式是基于概率的完备性原则。
的定义: 表示给定数据点 和当前模型参数 ,数据点 来自第 个高斯分量的后验概率 。 - 隐变量的性质: 在 GMM 中,我们假设每一个数据点
都必然且只由 个高斯分量中的某一个生成。也就是说,对于每个数据点 ,其对应的隐变量 (表示它属于哪个分量)必然会是 中的一个确定值。 - 概率的归一化: 由于数据点
必然属于且仅属于一个分量,那么它来自所有可能分量的后验概率之和必须为 1。这就像任何一个事件在所有可能结果上的概率之和总是 1。
因此,
这确保了每个数据点的“责任”在所有分量上是完整分配的,没有遗漏或重复。
- 与 M 步中混合权重
更新的关系
这个特性在 M 步中更新混合权重 时起着至关重要的作用,它保证了更新后的权重是合理且规范化的。 在 M 步中,混合权重
的更新公式为: 其关系体现在以下几点:
- 分子的含义:
表示第 个高斯分量对整个数据集所有数据点所承担的“总责任”。我们可以将其理解为第 个簇所“拥有”的有效数据点数量(因为 是软分配)。 - 分母的含义:
是数据集中的总数据点数量。 的物理意义: 代表第 个高斯分量在整个混合模型中所占的比例或先验概率。 - 总责任的守恒: 我们可以验证所有簇的总责任之和等于总数据点数
: 由于 (每个数据点的责任之和为 1),上式变为: 这意味着,所有分量“分享”的总有效数据点数量恰好等于实际的数据点总数 。 - 保证
的合理性: 将第 个簇的有效数据点数量 除以总数据点数 ,得到的 自然地表示了该簇在整个数据集中的比例。由于所有簇的有效数据点总数等于 ,这自动保证了所有更新后的混合权重之和为 1:
综上所述,
是 E 步中计算后验概率的基本性质,它确保了每个数据点的责任被完全分配。这个性质在 M 步中被巧妙地利用,使得混合权重 的更新公式能够准确地反映每个簇所“捕获”的数据点的比例,并且自动满足所有混合权重之和为 1 的必要条件。 - 为什么
GMM中EM算法的公式:
E步:
M步:
- K-means是如何对GMM进行初始化的?
运行 K-means: 首先,对数据运行 K-means 算法,得到
个硬性(明确划分的)簇和它们的质心。 设置 GMM 初始参数:
- 均值 (
): 将每个高斯分量的初始均值设置为对应 K-means 簇的质心。 - 协方差 (
): 将每个高斯分量的初始协方差设置为对应 K-means 簇内数据点的样本协方差。 - 混合系数 (
): 将每个高斯分量的初始混合系数设置为对应 K-means 簇中数据点数量占总数据点数量的比例。
Summary of a GMMs by the EM Algorithm proof.
- We start from representing data likelihood
- Then look at sample likelihood
- Introducing a latent variable
and its latent distribution - We found out that an alternative way to approximate MLE: maximizing the ELBO of the data likelihood
- The E-step finds one representation of the ELBO, by equalizing
with the posterior responsibility - The M-step then maximizing this ELBO through zero-derivatives, therefore leading to the solution of parameters
, namely,
- We start from representing data likelihood
Ensemble Learning
- Prove that the bound of training error is
where
Solution: if
, then training error . The term
is related to the time , so:
- As the training progresses, the upper bound of the training error will reduce exponentially (fast training).
- Convergence? Two conditions:
- Training error goes to
. - Or,
, equivalently, . Boosting gets stuck: the boosting weights on training set are in such a way that every weak learner has 50% error.
What is the meaning of "Bootstrap" in Ensemble Learning?
在集成学习(Ensemble Learning)中,“Bootstrap”通常指的是自助采样法,它是Bagging(Bootstrap Aggregating) 这种集成学习方法的核心组成部分。
Bootstrap 是一种有放回的随机抽样方法。它的基本思想是从原始数据集中反复地、有放回地抽取与原始数据集大小相同(或近似相同)的样本集。
具体来说,对于一个包含
个样本的原始数据集 : - 有放回抽样: 从
中随机抽取一个样本,并将其添加到新的样本集 中。 - 重复
次: 重复步骤 1 共 次。 - 生成新的数据集: 最终得到的
就是一个自助采样集。 的大小与 相同,但由于是有放回抽样,它可能包含 中重复的样本,也可能缺失 中的一些样本(估计约有 36.8% 的原始样本不会出现在 中)。
通过重复这个过程多次,我们可以生成多个不同的自助采样集
。 - 有放回抽样: 从