IncrementalPCA#
- class sklearn.decomposition.IncrementalPCA(n_components=None, *, whiten=False, copy=True, batch_size=None)[source]#
增量主成分分析 (IPCA)。
使用数据的奇异值分解进行线性降维,仅保留最显著的奇异向量将数据投影到较低维空间。在应用奇异值分解之前,输入数据按特征进行中心化,但不进行缩放。
根据输入数据的大小,此算法比 PCA 具有更高的内存效率,并允许稀疏输入。
此算法具有恒定的内存复杂度,约为
batch_size * n_features,因此无需将整个文件加载到内存中即可使用 np.memmap 文件。对于稀疏矩阵,输入会分批转换为密集矩阵(以便能够减去均值),从而避免在任何时候存储整个密集矩阵。每次 SVD 的计算开销为
O(batch_size * n_features ** 2),但一次只有 2 * batch_size 个样本保留在内存中。为了获取主成分,将有n_samples / batch_size次 SVD 计算,而 PCA 需要进行 1 次大的 SVD 计算,复杂度为O(n_samples * n_features ** 2)。有关使用示例,请参见 增量 PCA。
在 用户指南 中阅读更多内容。
在版本 0.16 中新增。
- 参数:
- n_componentsint, default=None
要保留的组件数。如果
n_components为None,则n_components设置为min(n_samples, n_features)。- whitenbool, default=False
当为 True(默认为 False)时,
components_向量除以n_samples乘以components_,以确保输出不相关且具有单位组件方差。白化会从转换后的信号中去除一些信息(组件的相对方差尺度),但有时可以通过使数据符合某些硬编码假设来提高下游估计器的预测准确性。
- copy布尔值, 默认为 True
如果为 False,X 将被覆盖。
copy=False可用于节省内存,但对于一般使用来说不安全。- batch_sizeint, default=None
用于每个批次的样本数。仅在调用
fit时使用。如果batch_size为None,则batch_size会从数据中推断出来并设置为5 * n_features,以在近似准确性和内存消耗之间取得平衡。
- 属性:
- components_ndarray of shape (n_components, n_features)
特征空间中的主轴,代表数据中最大方差的方向。等效于中心化输入数据的右奇异向量,平行于其特征向量。组件按
explained_variance_降序排列。- explained_variance_shape 为 (n_components,) 的 ndarray
每个选定组件所解释的方差。
- explained_variance_ratio_shape 为 (n_components,) 的 ndarray
每个选定组件所解释的方差百分比。如果存储了所有组件,则解释方差的总和等于 1.0。
- singular_values_shape 为 (n_components,) 的 ndarray
与每个选定分量对应的奇异值。奇异值等于较低维空间中
n_components个变量的 2-范数。- mean_shape 为 (n_features,) 的 ndarray
每个特征的经验均值,在对
partial_fit的调用中累加。- var_ndarray of shape (n_features,)
每个特征的经验方差,在对
partial_fit的调用中累加。- noise_variance_float
根据 Tipping 和 Bishop 1999 年的概率 PCA 模型估计的噪声协方差。参见 C. Bishop 的《模式识别与机器学习》,第 12.2.1 节,第 574 页或 http://www.miketipping.com/papers/met-mppca.pdf。
- n_components_int
估计的组件数。当
n_components=None时相关。- n_samples_seen_int
估计器处理的样本数。在新调用 fit 时会重置,但在
partial_fit调用中会增加。- batch_size_int
从
batch_size推断出的批次大小。- n_features_in_int
在 拟合 期间看到的特征数。
0.24 版本新增。
- feature_names_in_shape 为 (
n_features_in_,) 的 ndarray 在 fit 期间看到的特征名称。仅当
X具有全部为字符串的特征名称时才定义。1.0 版本新增。
另请参阅
PCA主成分分析 (PCA)。
KernelPCA核主成分分析 (KPCA)。
SparsePCA稀疏主成分分析 (SparsePCA)。
TruncatedSVD使用截断 SVD 进行降维。
注意事项
实现了 Ross 等人 (2008) [1] 的增量 PCA 模型。此模型是 Levy 和 Lindenbaum (2000) [2] 的 Sequential Karhunen-Loeve Transform 的扩展。
我们特意没有采用这两篇论文作者使用的一种优化方法,即在特定情况下用于降低 SVD 算法复杂度的 QR 分解。该技术的来源是《矩阵计算》(Golub and Van Loan 1997 [3])。省略此技术是因为它仅在分解矩阵的
n_samples(行)>= 5/3 *n_features(列)时才具有优势,并且会损害已实现算法的可读性。如果认为有必要,这将是未来优化的一个好机会。References
[1]D. Ross, J. Lim, R. Lin, M. Yang. Incremental Learning for Robust Visual Tracking, International Journal of Computer Vision, Volume 77, Issue 1-3, pp. 125-141, May 2008. https://www.cs.toronto.edu/~dross/ivt/RossLimLinYang_ijcv.pdf
[2][3]G. Golub and C. Van Loan. Matrix Computations, Third Edition, Chapter 5, Section 5.4.4, pp. 252-253, 1997.
示例
>>> from sklearn.datasets import load_digits >>> from sklearn.decomposition import IncrementalPCA >>> from scipy import sparse >>> X, _ = load_digits(return_X_y=True) >>> transformer = IncrementalPCA(n_components=7, batch_size=200) >>> # either partially fit on smaller batches of data >>> transformer.partial_fit(X[:100, :]) IncrementalPCA(batch_size=200, n_components=7) >>> # or let the fit function itself divide the data into batches >>> X_sparse = sparse.csr_matrix(X) >>> X_transformed = transformer.fit_transform(X_sparse) >>> X_transformed.shape (1797, 7)
- fit(X, y=None)[source]#
使用大小为 batch_size 的小批次拟合模型。
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练数据,其中
n_samples是样本数,n_features是特征数。- y被忽略
未使用,按照惯例为保持 API 一致性而存在。
- 返回:
- selfobject
返回实例本身。
- fit_transform(X, y=None, **fit_params)[source]#
拟合数据,然后对其进行转换。
使用可选参数
fit_params将转换器拟合到X和y,并返回X的转换版本。- 参数:
- Xshape 为 (n_samples, n_features) 的 array-like
输入样本。
- y形状为 (n_samples,) 或 (n_samples, n_outputs) 的类数组对象,默认=None
目标值(对于无监督转换,为 None)。
- **fit_paramsdict
额外的拟合参数。仅当估计器在其
fit方法中接受额外的参数时才传递。
- 返回:
- X_newndarray array of shape (n_samples, n_features_new)
转换后的数组。
- get_covariance()[source]#
使用生成模型计算数据协方差。
cov = components_.T * S**2 * components_ + sigma2 * eye(n_features),其中 S**2 包含解释方差,sigma2 包含噪声方差。- 返回:
- covarray of shape=(n_features, n_features)
估计的数据协方差。
- get_feature_names_out(input_features=None)[source]#
获取转换的输出特征名称。
The feature names out will prefixed by the lowercased class name. For example, if the transformer outputs 3 features, then the feature names out are:
["class_name0", "class_name1", "class_name2"].- 参数:
- input_featuresarray-like of str or None, default=None
Only used to validate feature names with the names seen in
fit.
- 返回:
- feature_names_outstr 对象的 ndarray
转换后的特征名称。
- get_metadata_routing()[source]#
获取此对象的元数据路由。
请查阅 用户指南,了解路由机制如何工作。
- 返回:
- routingMetadataRequest
封装路由信息的
MetadataRequest。
- get_params(deep=True)[source]#
获取此估计器的参数。
- 参数:
- deepbool, default=True
如果为 True,将返回此估计器以及包含的子对象(如果它们是估计器)的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- get_precision()[source]#
使用生成模型计算数据精度矩阵。
等于协方差的逆矩阵,但使用矩阵求逆引理计算以提高效率。
- 返回:
- precisionarray, shape=(n_features, n_features)
估计的数据精度。
- inverse_transform(X)[source]#
将数据转换回其原始空间。
换句话说,返回一个输入
X_original,其变换结果为 X。- 参数:
- Xarray-like of shape (n_samples, n_components)
新数据,其中
n_samples是样本数,n_components是组件数。
- 返回:
- X_originalarray-like of shape (n_samples, n_features)
原始数据,其中
n_samples是样本数量,n_features是特征数量。
注意事项
如果启用了白化,inverse_transform 将计算精确的逆操作,包括反向白化。
- partial_fit(X, y=None, check_input=True)[source]#
使用 X 进行增量拟合。X 中的所有内容都作为一个批次进行处理。
- 参数:
- Xshape 为 (n_samples, n_features) 的 array-like
训练数据,其中
n_samples是样本数,n_features是特征数。- y被忽略
未使用,按照惯例为保持 API 一致性而存在。
- check_inputbool, default=True
对 X 运行 check_array。
- 返回:
- selfobject
返回实例本身。
- set_output(*, transform=None)[source]#
设置输出容器。
有关如何使用 API 的示例,请参阅引入 set_output API。
- 参数:
- transform{“default”, “pandas”, “polars”}, default=None
配置
transform和fit_transform的输出。"default": 转换器的默认输出格式"pandas": DataFrame 输出"polars": Polars 输出None: 转换配置保持不变
1.4 版本新增: 添加了
"polars"选项。
- 返回:
- selfestimator instance
估计器实例。
- set_params(**params)[source]#
设置此估计器的参数。
此方法适用于简单的估计器以及嵌套对象(如
Pipeline)。后者具有<component>__<parameter>形式的参数,以便可以更新嵌套对象的每个组件。- 参数:
- **paramsdict
估计器参数。
- 返回:
- selfestimator instance
估计器实例。
- transform(X)[source]#
对 X 应用降维。
如果 X 是稀疏的,X 将投影到先前从训练集中提取的前几个主成分上,使用大小为 batch_size 的小批次。
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
新数据,其中
n_samples是样本数量,n_features是特征数量。
- 返回:
- X_newndarray of shape (n_samples, n_components)
X 在前几个主成分上的投影。
示例
>>> import numpy as np >>> from sklearn.decomposition import IncrementalPCA >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], ... [1, 1], [2, 1], [3, 2]]) >>> ipca = IncrementalPCA(n_components=2, batch_size=3) >>> ipca.fit(X) IncrementalPCA(batch_size=3, n_components=2) >>> ipca.transform(X)