我试图找出如何在Python中重现我在SAS中所做的一些工作。使用这个存在多重共线性问题的数据集,我想在Python中执行主成分分析。我看过scikit-learn和statsmodels,但是我不确定如何获取它们的输出并将其转换为与SAS相同的结果结构。一方面,当您使用时,SAS似乎在相关矩阵上执行PCA PROC PRINCOMP
,但是大多数(全部?)Python库似乎都在使用SVD。
在数据集中,第一列是响应变量,接下来的5个是预测变量,称为pred1-pred5。
在SAS中,常规工作流程为:
/* Get the PCs */
proc princomp data=indata out=pcdata;
var pred1 pred2 pred3 pred4 pred5;
run;
/* Standardize the response variable */
proc standard data=pcdata mean=0 std=1 out=pcdata2;
var response;
run;
/* Compare some models */
proc reg data=pcdata2;
Reg: model response = pred1 pred2 pred3 pred4 pred5 / vif;
PCa: model response = prin1-prin5 / vif;
PCfinal: model response = prin1 prin2 / vif;
run;
quit;
/* Use Proc PLS to to PCR Replacement - dropping pred5 */
/* This gets me my parameter estimates for the original data */
proc pls data=indata method=pcr nfac=2;
model response = pred1 pred2 pred3 pred4 / solution;
run;
quit;
我知道最后一步只能奏效,因为我只依次选择了PC1和PC2。
因此,在Python中,这与我所了解的差不多:
import pandas as pd
import numpy as np
from sklearn.decomposition.pca import PCA
source = pd.read_csv('C:/sourcedata.csv')
# Create a pandas DataFrame object
frame = pd.DataFrame(source)
# Make sure we are working with the proper data -- drop the response variable
cols = [col for col in frame.columns if col not in ['response']]
frame2 = frame[cols]
pca = PCA(n_components=5)
pca.fit(frame2)
每台PC解释的差异量是多少?
print pca.explained_variance_ratio_
Out[190]:
array([ 9.99997603e-01, 2.01265023e-06, 2.70712663e-07,
1.11512302e-07, 2.40310191e-09])
这些是什么?特征向量?
print pca.components_
Out[179]:
array([[ -4.32840645e-04, -7.18123771e-04, -9.99989955e-01,
-4.40303223e-03, -2.46115129e-05],
[ 1.00991662e-01, 8.75383248e-02, -4.46418880e-03,
9.89353169e-01, 5.74291257e-02],
[ -1.04223303e-02, 9.96159390e-01, -3.28435046e-04,
-8.68305757e-02, -4.26467920e-03],
[ -7.04377522e-03, 7.60168675e-04, -2.30933755e-04,
5.85966587e-02, -9.98256573e-01],
[ -9.94807648e-01, -1.55477793e-03, -1.30274879e-05,
1.00934650e-01, 1.29430210e-02]])
这些是特征值吗?
print pca.explained_variance_
Out[180]:
array([ 8.07640319e+09, 1.62550137e+04, 2.18638986e+03,
9.00620474e+02, 1.94084664e+01])
我对如何从Python结果到实际执行主成分回归(在Python中)感到有些困惑。是否有任何Python库类似于SAS来填充空白?
任何提示表示赞赏。在SAS输出中使用标签让我有些受宠若惊,而且我对熊猫,numpy,scipy或scikit-learn不太熟悉。
编辑:
因此,看起来sklearn不会直接在熊猫数据框上运行。假设我将其转换为numpy数组:
npa = frame2.values
npa
这是我得到的:
Out[52]:
array([[ 8.45300000e+01, 4.20730000e+02, 1.99443000e+05,
7.94000000e+02, 1.21100000e+02],
[ 2.12500000e+01, 2.73810000e+02, 4.31180000e+04,
1.69000000e+02, 6.28500000e+01],
[ 3.38200000e+01, 3.73870000e+02, 7.07290000e+04,
2.79000000e+02, 3.53600000e+01],
...,
[ 4.71400000e+01, 3.55890000e+02, 1.02597000e+05,
4.07000000e+02, 3.25200000e+01],
[ 1.40100000e+01, 3.04970000e+02, 2.56270000e+04,
9.90000000e+01, 7.32200000e+01],
[ 3.85300000e+01, 3.73230000e+02, 8.02200000e+04,
3.17000000e+02, 4.32300000e+01]])
如果我随后将copy
sklearn的PCA 的参数更改为False,
直接在数组上运行,请按照以下注释操作。
pca = PCA(n_components=5,copy=False)
pca.fit(npa)
npa
对于输出,看起来它替换了所有值,npa
而不是在数组中附加任何内容。现在的价值是npa
什么?原始数组的主成分得分是多少?
Out[64]:
array([[ 3.91846649e+01, 5.32456568e+01, 1.03614689e+05,
4.06726542e+02, 6.59830027e+01],
[ -2.40953351e+01, -9.36743432e+01, -5.27103110e+04,
-2.18273458e+02, 7.73300268e+00],
[ -1.15253351e+01, 6.38565684e+00, -2.50993110e+04,
-1.08273458e+02, -1.97569973e+01],
...,
[ 1.79466488e+00, -1.15943432e+01, 6.76868901e+03,
1.97265416e+01, -2.25969973e+01],
[ -3.13353351e+01, -6.25143432e+01, -7.02013110e+04,
-2.88273458e+02, 1.81030027e+01],
[ -6.81533512e+00, 5.74565684e+00, -1.56083110e+04,
-7.02734584e+01, -1.18869973e+01]])
copy=False
,则会得到新值。这些是主要成分分数吗?