Python for data analysis----linear regression

Source: Internet
Author: User
Tags vars statsmodels

Linear regression Analysis:

method: Import Statsmodels.api asSmimport Pandas asPD frompatsy.highlevel Import dmatrices----2.7 inside is from Patsy import dmatricesHG='D:/hg.csv'DF=Pd.read_csv (Hg) VARs=['Rkzzl','ZRS','RJGDP']DF=df[vars]y,x=dmatrices ('Rkzzl ~ zrs + RJGDP', data=df,return_type='Dataframe') MoD=SM. OLS (y,x) () print res.summary ()

All code:

Import Statsmodels.api asSmimport Pandas asPdimport NumPy asNP frompatsy.highlevel Import dmatrices fromcommon.util.my_sqlalchemy Import Sqlalchemy_engineimport mathsql="Select Q1r3, Q1R5, Q1R6, q1r7 from Db2017091115412316222027656281_1;"DF=pd.read_sql (SQL, sqlalchemy_engine) Df_dropna=Df.dropna () y,x=dmatrices ('Q1R3 ~ q1r5 + q1r6 + q1r7', data=df_dropna,return_type='Dataframe') MoD=SM. OLS (y,x) () result=res.summary () print (result) model= {                'N':int(res.nobs),'DF': Res.df_model,'R': Math.sqrt (res.rsquared),'r_squared': res.rsquared,'R_squared_adj': Res.rsquared_adj,'f_statistic': Res.fvalue,'prob_f_statistic': Res.f_pvalue,}coefficient= {         'coefficient': List (res.params),         'STD': List (Np.diag (np.sqrt (Res.cov_params ))),'T': List (res.tvalues),'Sig': [I forIinchMap (lambda x:float(x), ("". Join ("{:. 4f},"*len (res.pvalues)). Format (*list (res.pvalues)). Rstrip (","). Split (",")]}returnvalue= {'Model': Model,'coefficient': Coefficient}print (returnvalue)

{    'Model': {            'DF':3.0,             'N':665,             'prob_f_statistic':1.185607423551511E-17,             'R_squared_adj':0.11247707470462853,             'f_statistic':29.049896130483212,             'r_squared':0.11648696743939679,             'R':0.3413018714267427},     'coefficient': {            'STD': [0.30170364007280126,0.049972399035516278,0.051623405028706125,0.047659986606566104],             'Sig': [0.0,0.0,0.0,0.0312],             'T': [5.4578212730306044,5.3469744215460269,4.3810228293129168,2.1587543885465008],             'coefficient': [1.6466445449401035,0.26720113942619689,0.22616331595762876,0.10288620524499202]}}

Python for data analysis----linear regression

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.