Python – 主成分分析的变体
主成分分析 (PCA)是一种无监督的降维和可视化技术。它通常被称为线性技术,因为新特征的映射是由特征乘以 PCA 特征向量矩阵给出的。它的工作原理是简单地识别靠近数据的超平面,然后将数据投影到它上面以最大化方差。由于 PCA 遵循的简单方法,它被广泛用于数据挖掘、生物信息学、心理学等。我们大多数人都不知道这个算法有各种版本,它们比传统方法更好。让我们一一看看。
随机 PCA:
这是 PCA 的扩展,它使用数据的近似奇异值分解 (SVD)。常规 PCA 的工作时间为 O(n*p 2 ) + O(p 3 ),其中n是数据点的数量, p是特征的数量,而随机版本的工作时间为 O(n*d*2) + O(d 3 ) 其中 d 是主成分的数量。因此,当d远小于n时,它的速度非常快。
sklearn 在sklearn.utils.extmath中提供了一个 random_svd 方法,可用于进行随机 PCA。此方法返回三个矩阵: U是mxm矩阵,Sigma 是mxn对角矩阵, V^T是nxn矩阵的转置,其中 T 是上标。另一种使用sklearn.decomposition.PCA并将 svd_solver 超参数从“auto”更改为“randomized”或“full”的方法。但是,如果p或n超过 500 或主成分的数量小于p和n的 80%,Scikit-learn 会自动使用随机 PCA。
代码:
Python3
# Python3 program to show the working of
# randomized PCA
# importing libraries
import numpy as np
from sklearn.decomposition import PCA
from sklearn.utils.extmath import randomized_svd
# dummy data
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
# creates instance of PCA with randomized svd_solver
pca = PCA(n_components = 2, svd_solver ='randomized')
# This function takes a matrix and returns the
# U, Sigma and V ^ T elements
U, S, VT = randomized_svd(X, n_components = 2)
# matrix returned by randomized_svd
print(f"Matrix U of size m * m: {U}\n")
print(f"Matrix S of size m * n: {S}\n")
print(f"Matrix V ^ T of size n * n: {VT}\n")
# fitting the pca model
pca.fit(X)
# printing the explained variance ratio
print("Explained Variance using PCA with randomized svd_solver:", pca.explained_variance_ratio_)
Python3
# Python3 program to show the working of
# incremental PCA
# importing libraries
import numpy as np
from sklearn.decomposition import IncrementalPCA
# dummy data
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
# specify the number of batches
no_of_batches = 3
# create an instance of IncrementalPCA
incremental_pca = IncrementalPCA(n_components = 2)
# fit the data in batches
for batch in np.array_split(X, no_of_batches):
incremental_pca.fit(batch)
# fit and transform the data
final = incremental_pca.transform(X)
# prints an 2d-array (as n_components = 2)
print(final)
Python3
# Python3 program to show the working of
# Kernel PCA
# importing libraries
import numpy as np
from sklearn.decomposition import KernelPCA
# dummy data
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
# creating an instance of KernelPCA using rbf kernel
kernel_pca = KernelPCA(n_components = 2, kernel ="rbf", gamma = 0.03)
# fit and transform the data
final = kernel_pca.fit_transform(X)
# prints an 2d-array (as n_components = 2)
print(final)
输出:
Matrix U of size m*m: [[ 0.21956688 -0.53396977]
[ 0.35264795 0.45713538]
[ 0.57221483 -0.07683439]
[-0.21956688 0.53396977]
[-0.35264795 -0.45713538]
[-0.57221483 0.07683439]]
Matrix S of size m*n: [6.30061232 0.54980396]
Matrix V^T of size n*n: [[-0.83849224 -0.54491354]
[-0.54491354 0.83849224]]
Explained Variance using PCA with randomized svd_solver: [0.99244289 0.00755711]
增量 PCA:
PCA 和大多数降维算法的主要问题是它们需要一次性将整个数据放入内存中,并且由于数据有时非常庞大,因此很难放入内存中。
幸运的是,有一种称为增量 PCA 的算法对于大型训练数据集很有用,因为它将数据分成最小批次,并一次将其提供给增量 PCA。这称为动态学习。由于一次没有多少数据存在于内存中,因此内存使用受到控制。
Scikit-Learn 为我们提供了一个名为 sklearn.decomposition.IncrementalPCA 的类,我们可以使用它来实现它。
代码:
Python3
# Python3 program to show the working of
# incremental PCA
# importing libraries
import numpy as np
from sklearn.decomposition import IncrementalPCA
# dummy data
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
# specify the number of batches
no_of_batches = 3
# create an instance of IncrementalPCA
incremental_pca = IncrementalPCA(n_components = 2)
# fit the data in batches
for batch in np.array_split(X, no_of_batches):
incremental_pca.fit(batch)
# fit and transform the data
final = incremental_pca.transform(X)
# prints an 2d-array (as n_components = 2)
print(final)
输出:
[[-4.24264069e+00 7.07106781e-01]
[-4.94974747e+00 1.41421356e+00]
[-6.36396103e+00 1.41421356e+00]
[-1.41421356e+00 7.07106781e-01]
[-7.07106781e-01 -5.55111512e-17]
[ 7.07106781e-01 5.55111512e-17]]
核心 PCA:
Kernel PCA 是另一个使用内核的 PCA 扩展。内核是一种数学技术,我们可以使用它将实例映射到称为特征空间的非常高的维度空间,从而使用支持向量机 (SVM) 实现非线性分类和回归。这通常用于新奇检测和图像去噪。
Scikit-Learn 在 sklearn.decomposition 中提供了一个类 KernelPCA,可用于执行 Kernel PCA。
代码:
Python3
# Python3 program to show the working of
# Kernel PCA
# importing libraries
import numpy as np
from sklearn.decomposition import KernelPCA
# dummy data
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
# creating an instance of KernelPCA using rbf kernel
kernel_pca = KernelPCA(n_components = 2, kernel ="rbf", gamma = 0.03)
# fit and transform the data
final = kernel_pca.fit_transform(X)
# prints an 2d-array (as n_components = 2)
print(final)
输出:
[[-0.3149893 -0.17944928]
[-0.46965347 -0.0475298 ]
[-0.62541667 0.22697909]
[ 0.3149893 -0.17944928]
[ 0.46965347 -0.0475298 ]
[ 0.62541667 0.22697909]]
KernelPCA 是无监督的,因此没有明显的措施来选择最佳内核。由于我们通常使用降维作为监督学习算法中的一个步骤,因此我们可以使用带有 GridSearchCV 的管道来选择最佳超参数,然后使用这些超参数(内核和伽马)来获得最佳分类精度。