1.2. 线性和二次鉴别分析#
线性判别分析 (LinearDiscriminantAnalysis
)和二次鉴别分析 (QuadraticDiscriminantAnalysis
)是两个经典的分类器,顾名思义,分别具有线性和二次决策面。
这些分类器之所以有吸引力,是因为它们具有易于计算的封闭形式的解决方案,本质上是多类的,已被证明在实践中工作良好,并且没有超参数可供调整。
伊达达
该图显示了线性鉴别分析和二次鉴别分析的决策边界。最下面的一行表明线性鉴别分析只能学习线性边界,而二次鉴别分析可以学习二次边界,因此更灵活。
示例
具有协方差椭圆体的线性和二次鉴别分析 :合成数据上LDA和QDA的比较。
1.2.1. 使用线性鉴别分析降低模糊性#
LinearDiscriminantAnalysis
可用于通过将输入数据投影到线性子空间来执行有监督的维度约简,线性子空间由最大化类之间分离的方向组成(在下面的数学部分中讨论的精确意义上)。输出的维度必然小于类的数量,因此这通常是一种相当强的维度缩减,并且只有在多类设置中才有意义。
这是在 transform
法可以使用 n_components
参数.该参数对 fit
和 predict
方法.
示例
Iris数据集LDA和PCA 2D投影的比较 :LDA和PCA对Iris数据集进行降维的比较
1.2.2. LDA和QDA分类器的数学公式#
LDA和QDA都可以从简单的概率模型中推导出来,这些模型对数据的类别条件分布进行建模 \(P(X|y=k)\) 为每个类 \(k\) .然后可以通过使用贝叶斯规则对每个训练样本进行预测 \(x \in \mathcal{R}^d\) :
然后我们选择班级 \(k\) 这最大化了这个后验概率。
更具体地说,对于线性和二次判定分析, \(P(x|y)\) 被建模为具有密度的多元高斯分布:
哪里 \(d\) 是功能的数量。
1.2.2.1. QDA#
根据上面的模型,后验的log为:
其中常数项 \(Cst\) 对应于分母 \(P(x)\) ,除了高斯的其他常项之外。预测的类别是最大化该日志后验的类别。
备注
Relation with Gaussian Naive Bayes
如果在QDA模型中假设协方差矩阵是对角的,那么假设输入在每个类别中有条件地独立,并且所得分类器等效于高斯朴素Bayes分类器 naive_bayes.GaussianNB
.
1.2.2.2. LDA#
LDA是QDA的一种特例,其中假设每个类别的高斯共享相同的协方差矩阵: \(\Sigma_k = \Sigma\) 为所有 \(k\) .这将日志减少到:
The term \((x-\mu_k)^t \Sigma^{-1} (x-\mu_k)\) corresponds to the Mahalanobis Distance between the sample \(x\) and the mean \(\mu_k\). The Mahalanobis distance tells how close \(x\) is from \(\mu_k\), while also accounting for the variance of each feature. We can thus interpret LDA as assigning \(x\) to the class whose mean is the closest in terms of Mahalanobis distance, while also accounting for the class prior probabilities.
LDA的log后验也可以写成 [3] 作为:
where \(\omega_k = \Sigma^{-1} \mu_k\) and \(\omega_{k0} =
-\frac{1}{2} \mu_k^t\Sigma^{-1}\mu_k + \log P (y = k)\). These quantities
correspond to the coef_
and intercept_
attributes, respectively.
从上面的公式可以看出,LDA具有线性决策面。就QDA而言,协方差矩阵没有任何假设 \(\Sigma_k\) 高斯定律,从而导致二次决策面。看到 [1] 了解更多详细信息。
1.2.3. LDA降维的数学公式#
首先注意K的意思是 \(\mu_k\) 是载体 \(\mathcal{R}^d\) ,并且它们位于仿射子空间中 \(H\) 最多维度 \(K - 1\) (2个点位于一条直线上,3个点位于一个平面上,等等)。
As mentioned above, we can interpret LDA as assigning \(x\) to the class whose mean \(\mu_k\) is the closest in terms of Mahalanobis distance, while also accounting for the class prior probabilities. Alternatively, LDA is equivalent to first sphering the data so that the covariance matrix is the identity, and then assigning \(x\) to the closest mean in terms of Euclidean distance (still accounting for the class priors).
在这个d维空间中计算欧几里得距离相当于首先将数据点投影到 \(H\) ,并计算那里的距离(因为其他维度在距离方面对每个类别的贡献相等)。换句话说如果 \(x\) 最接近 \(\mu_k\) 在原来的空间中,也会是这样 \(H\) .这表明,在LDA分类器中,通过线性投影到 \(K-1\) 维度空间。
我们可以进一步缩小维度,到一个选择 \(L\) 通过投影到线性子空间 \(H_L\) 这最大化了 \(\mu^*_k\) 投影后(实际上,我们正在为转换后的类平均值进行一种形式的PCA \(\mu^*_k\) ).这 \(L\) 对应于 n_components
中使用的参数 transform
法看到 [1] 了解更多详细信息。
1.2.4. 收缩和协方差估计#
收缩是一种正规化形式,用于在训练样本数量与特征数量相比较小的情况下改进协方差矩阵的估计。在这种情况下,经验样本协方差是一个较差的估计器,而收缩有助于提高分类器的概括性能。可以通过设置 shrinkage
参数 LinearDiscriminantAnalysis
类来 'auto'
.这遵循Ledoit和Wolf引入的引理,以分析方式自动确定最佳收缩参数 [2]. 请注意,目前收缩仅在设置 solver
参数以 'lsqr'
或 'eigen'
.
的 shrinkage
参数也可以在0和1之间手动设置。特别是,0值对应于无收缩(这意味着将使用经验协方差矩阵),1值对应于完全收缩(这意味着方差对角矩阵将被用作协方差矩阵的估计)。将此参数设置为这两个极端之间的值将估计协方差矩阵的缩小版本。
缩小的Ledoit和Wolf协方差估计量可能并不总是最好的选择。例如,如果数据的分布呈正态分布,则Oracle逼近收缩估计器 sklearn.covariance.OAS
产生的均方误差比Ledoit和Wolf的公式给出的更小 shrinkage="auto"
.在LDA中,假设数据对于类来说是有条件的高斯。如果这些假设成立,则将LDA与OAS协方差估计量一起使用将产生比使用Ledoit和Wolf或经验协方差估计量更好的分类准确性。
可以使用 covariance_estimator
参数 discriminant_analysis.LinearDiscriminantAnalysis
课协方差估计器应该有 fit 方法和 covariance_
属性,类似于中的所有协方差估计量 sklearn.covariance
module.
收缩
示例
用于分类的正态、Ledoit-Wolf和OAS线性鉴别分析 :LDA分类器与Empirical、Ledoit Wolf和OAS协方差估计器的比较。
1.2.5. 估计算法#
使用LDA和QDA需要计算依赖于类先验的对数后验 \(P(y=k)\) ,班级意味着 \(\mu_k\) 和协方差矩阵。
The 'svd' solver is the default solver used for
LinearDiscriminantAnalysis
, and it is
the only available solver for
QuadraticDiscriminantAnalysis
.
It can perform both classification and transform (for LDA).
As it does not rely on the calculation of the covariance matrix, the 'svd'
solver may be preferable in situations where the number of features is large.
The 'svd' solver cannot be used with shrinkage.
For QDA, the use of the SVD solver relies on the fact that the covariance
matrix \(\Sigma_k\) is, by definition, equal to \(\frac{1}{n - 1}
X_k^tX_k = \frac{1}{n - 1} V S^2 V^t\) where \(V\) comes from the SVD of the (centered)
matrix: \(X_k = U S V^t\). It turns out that we can compute the
log-posterior above without having to explicitly compute \(\Sigma\):
computing \(S\) and \(V\) via the SVD of \(X\) is enough. For
LDA, two SVDs are computed: the SVD of the centered input matrix \(X\)
and the SVD of the class-wise mean vectors.
The 'lsqr'
solver is an efficient algorithm that only works for
classification. It needs to explicitly compute the covariance matrix
\(\Sigma\), and supports shrinkage and custom covariance estimators.
This solver computes the coefficients
\(\omega_k = \Sigma^{-1}\mu_k\) by solving for \(\Sigma \omega =
\mu_k\), thus avoiding the explicit computation of the inverse
\(\Sigma^{-1}\).
的 'eigen'
求解器基于类间散布率与类内散布率的优化。它可以用于分类和转换,并且支持收缩。但 'eigen'
求解程序需要计算协方差矩阵,因此它可能不适合具有大量要素的情况。
引用