数学建模用python分析gdp_【主成分分析】《数学建模算法与应用》第十章 多元分析 第二节 主成分分析 python实现...

本文通过Python实现主成分分析(PCA),以数学建模中的GDP数据为例,进行线性回归分析,展示了PCA在降低数据维度、解释变量贡献率上的效果。通过PCA处理后的模型保持了高解释力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

x0=np.array(sn.iloc[:,0:n-1])

y0=np.array(sn.iloc[:,n-1])

results = sm.OLS(y0, np.c_[np.ones((m,1)),x0]).fit()

print(results.summary())

OLS Regression Results

==============================================================================

Dep. Variable: y R-squared: 0.982

Model: OLS Adj. R-squared: 0.974

Method: Least Squares F-statistic: 111.5

Date: Fri, 31 Jul 2020 Prob (F-statistic): 4.76e-07

Time: 01:16:14 Log-Likelihood: -26.918

No. Observations: 13 AIC: 63.84

Df Residuals: 8 BIC: 66.66

Df Model: 4

Covariance Type: nonrobust

==============================================================================

coef std err t P>|t| [0.025 0.975]

------------------------------------------------------------------------------

const 62.4054 70.071 0.891 0.399 -99.179 223.989

x1 1.5511 0.745 2.083 0.071 -0.166 3.269

x2 0.5102 0.724 0.705 0.501 -1.159 2.179

x3 0.1019 0.755 0.135 0.896 -1.638 1.842

x4 -0.1441 0.709 -0.203 0.844 -1.779 1.491

==============================================================================

Omnibus: 0.165 Durbin-Watson: 2.053

Prob(Omnibus): 0.921 Jarque-Bera (JB): 0.320

Skew: 0.201 Prob(JB): 0.852

Kurtosis: 2.345 Cond. No. 6.06e+03

==============================================================================

Warnings:

[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

[2] The condition number is large, 6.06e+03. This might indicate that there are

strong multicollinearity or other numerical problems.

D:\Program Files\anaconda\lib\site-packages\scipy\stats\stats.py:1450: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=13

"anyway, n=%i" % int(n))

r=np.corrcoef(x0.T)

xd=zscore(x0)

yd=zscore(y0)

pca=PCA()

pca.fit(xd)

PCA(copy=True, iterated_power='auto', n_components=None, random_state=None,

svd_solver='auto', tol=0.0, whiten=False)

pca.explained_variance_ratio_#计算各个变量的贡献率

array([5.58926009e-01, 3.94016518e-01, 4.66515373e-02, 4.05936433e-04])

num=3#由于我们使用jupyter演示程序,可能不好交互式

pca_=PCA(n_components=num)

xd_=pca_.fit_transform(xd)

results = sm.OLS(yd, np.c_[np.ones((m,1)),xd_]).fit()

print(results.summary())

#原MATLAB程序中还做了使系数全为正的处理,在此报告中,x1为负,但是其实只要转成正的就行

OLS Regression Results

==============================================================================

Dep. Variable: y R-squared: 0.982

Model: OLS Adj. R-squared: 0.976

Method: Least Squares F-statistic: 164.9

Date: Fri, 31 Jul 2020 Prob (F-statistic): 3.50e-08

Time: 01:16:16 Log-Likelihood: 7.7143

No. Observations: 13 AIC: -7.429

Df Residuals: 9 BIC: -5.169

Df Model: 3

Covariance Type: nonrobust

==============================================================================

coef std err t P>|t| [0.025 0.975]

------------------------------------------------------------------------------

const 2.012e-16 0.045 4.52e-15 1.000 -0.101 0.101

x1 -0.6570 0.030 -22.045 0.000 -0.724 -0.590

x2 0.0083 0.035 0.234 0.820 -0.072 0.089

x3 0.3028 0.103 2.935 0.017 0.069 0.536

==============================================================================

Omnibus: 0.246 Durbin-Watson: 1.943

Prob(Omnibus): 0.884 Jarque-Bera (JB): 0.416

Skew: 0.162 Prob(JB): 0.812

Kurtosis: 2.186 Cond. No. 3.46

==============================================================================

Warnings:

[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

D:\Program Files\anaconda\lib\site-packages\scipy\stats\stats.py:1450: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=13

"anyway, n=%i" % int(n))

xback=np.dot(xd_,pca.components_[:3])

results = sm.OLS(yd, np.c_[np.ones((m,1)),xback]).fit()

print(results.summary())

OLS Regression Results

==============================================================================

Dep. Variable: y R-squared: 0.982

Model: OLS Adj. R-squared: 0.976

Method: Least Squares F-statistic: 164.9

Date: Fri, 31 Jul 2020 Prob (F-statistic): 3.50e-08

Time: 01:16:23 Log-Likelihood: 7.7143

No. Observations: 13 AIC: -7.429

Df Residuals: 9 BIC: -5.169

Df Model: 3

Covariance Type: nonrobust

==============================================================================

coef std err t P>|t| [0.025 0.975]

------------------------------------------------------------------------------

const 2.012e-16 0.045 4.52e-15 1.000 -0.101 0.101

x1 0.5130 0.073 6.992 0.000 0.347 0.679

x2 0.2787 0.039 7.078 0.000 0.190 0.368

x3 -0.0608 0.070 -0.866 0.409 -0.220 0.098

x4 -0.4229 0.030 -13.871 0.000 -0.492 -0.354

==============================================================================

Omnibus: 0.246 Durbin-Watson: 1.943

Prob(Omnibus): 0.884 Jarque-Bera (JB): 0.416

Skew: 0.162 Prob(JB): 0.812

Kurtosis: 2.186 Cond. No. 8.35e+15

==============================================================================

Warnings:

[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

[2] The smallest eigenvalue is 4.17e-31. This might indicate that there are

strong multicollinearity problems or that the design matrix is singular.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值