python主成分对变量的贡献率_在Python中从新数据计算主成分

这篇博客介绍了如何在Python中使用IncrementalPCA进行主成分分析(PCA),通过transform函数将数据投影到主成分上。示例展示了如何对新数据进行PCA变换,并解释了变量加载与主成分的关系,强调在应用PCA前需要确保数据的mean为0。
摘要由CSDN通过智能技术生成

以下是可用的transform函数here:def transform(self, X):

"""Apply dimensionality reduction to X.

X is projected on the first principal components previously extracted

from a training set.

Parameters

X : array-like, shape (n_samples, n_features)

New data, where n_samples is the number of samples

and n_features is the number of features.

Returns

-

X_new : array-like, shape (n_samples, n_components)

Examples

>>> 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, copy=True, n_components=2, whiten=False)

>>> ipca.transform(X) # doctest: +SKIP

"""

check_is_fitted(self, ['mean_', 'components_'], all_or_any=all)

X = check_array(X)

if self.mean_ is not None:

X = X - self.mean_

X_transformed = np.dot(X, self.components_.T)

if self.whiten:

X_transformed /= np.sqrt(self.explained_variance_)

return X_transformed

变量加载是从pca.components_获得的组件。确保你的mean_是0,而{}是{},然后你就可以简单地得到这个矩阵,并在你想转换矩阵/向量的任何地方使用它。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值