Skip to main content
 首页 » 编程设计

python之PCA 重建 : statsmodels vs sklearn

2024年11月01日9shanyou

我正在尝试将我的 PCA 模型从 sklearn 转移到 statmodels。

使用 sklearn,我可以按以下方式重建我的数据:

# reconstruction using 2 principal components 
pca = PCA(n_components=2) 
pca.fit(X) 
 
# reconstruct the data 
X_reconst = np.dot(pca.transform(X)[:,:2], pca.components_[:2,:]) 

在 statsmodels 中做同样的事情会怎样? API 和命名法似乎完全不同。

提前致谢!

请您参考如下方法:

Statsmodels PCA 实现将数据投影保存为模型属性。

例如,

import statsmodels.api as sm 
 
# specifying two components and fitting data 
pca = sm.PCA(X, ncomp=2) 
X_reconst = pca.projection