python bp回归分析_回归分析之Python实现

微信公众号:数据皮皮侠如果你觉得该公众号对你有帮助,欢迎关注、推广和宣传

内容目录:回归分析  Python实现

      在统计学中,回归分析(regression analysis)指的是确定两种或两种以上变量间相互依赖的定量关系的一种统计分析方法。回归分析按照涉及的变量的多少,分为二元回归和多元回归分析;按照因变量的多少,可分为简单回归分析和多重回归分析;按照自变量和因变量之间的关系类型,可分为线性回归分析和非线性回归分析。本文只探讨线性回归分析。

一、简单的一元回归的理论

python bp回归分析_回归分析之Python实现_第1张图片

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

python bp回归分析_回归分析之Python实现_第2张图片

python bp回归分析_回归分析之Python实现_第3张图片

python bp回归分析_回归分析之Python实现_第4张图片

python bp回归分析_回归分析之Python实现_第5张图片

其中R平方可以被看做是y的样本变动中可以被x解释的部分。且R平方的值总在0到1之间。

二、多元回归的理论

python bp回归分析_回归分析之Python实现_第6张图片

三、使用Python实现线性回归

Python中常用回归分析函数介绍

python bp回归分析_回归分析之Python实现_第7张图片

使用函数包后,回归结果如下:

python bp回归分析_回归分析之Python实现_第8张图片

以下将提供一个多元回归分析的具体事例,供参考。

import pandas as pd
import scipy.stats as stats
import statsmodels.api as sm
import matplotlib.pyplot as plt
from statsmodels.formula.api import ols 
#导入各函数包

TRD_Index = pd.read_table('C:\\Users\\DELL\Desktop\\TRD_Index.txt',
\sep='\t')
#导入需要进行回归分析的文件数据

SHindex=TRD_Index[TRD_Index.Indexcd==1]
SZindex=TRD_Index[TRD_Index.Indexcd==399106]
SHRet=SHindex.Retindex
SZRet=SZindex.Retindex
SZRet.index=SHRet.index
#在数据中提取一些指标
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus']=False
#构建模型进行回归分析
model=sm.OLS(SHRet,sm.add_constant(SZRet)).fit()
print(model.summary())

python bp回归分析_回归分析之Python实现_第9张图片

model.fittedvalues[:5]
plt.scatter(model.fittedvalues,model.resid)

python bp回归分析_回归分析之Python实现_第10张图片

plt.xlabel('拟合值')
plt.ylabel('残差')
sm.qqplot(model.resid_pearson, stats.norm,line ='45')

python bp回归分析_回归分析之Python实现_第11张图片

plt.scatter(model.fittedvalues,model.resid_pearson**0.5)
plt.xlabel('拟合值')
plt.ylabel('标准化残差的平方根')

python bp回归分析_回归分析之Python实现_第12张图片

获取数据:

链接: https://pan.baidu.com/s/1N4gD99IJFUr8p0t7SP6cFA 

提取码: 2h1n

python bp回归分析_回归分析之Python实现_第13张图片 python bp回归分析_回归分析之Python实现_第14张图片

关注数据皮皮虾

本期作者:赵浩阳

python bp回归分析_回归分析之Python实现_第15张图片 python bp回归分析_回归分析之Python实现_第16张图片 python bp回归分析_回归分析之Python实现_第17张图片

你可能感兴趣的:(python,bp回归分析)