Python学习第十四周作业——综合练习

题目来源于该网站:

https://nbviewer.jupyter.org/github/schmit/cme193-ipython-notebooks-lecture/blob/master/Exercises.ipynb

%matplotlib inline

import random

import numpy as np
import scipy as sp
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

import statsmodels.api as sm
import statsmodels.formula.api as smf

sns.set_context("talk")
# Anscombe’s quartet Anscombe’s quartet comprises of four datasets, and is rather famous. Why? You’ll find out in this exercise.
anascombe = pd.read_csv('data/anscombe.csv')
anascombe.head()
datasetxy
0I108.04
1I86.95
2I137.58
3I98.81
4I118.33

Part 1

For each of the four datasets…
- Compute the mean and variance of both x and y
- Compute the correlation coefficient between x and y
- Compute the linear regression line: y=β0+β1x+ϵ y = β 0 + β 1 x + ϵ (hint: use statsmodels and look at the Statsmodels notebook)

%matplotlib inline

import random

import numpy as np
import scipy as sp
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

import statsmodels.api as sm
import statsmodels.formula.api as smf

anascombe = pd.read_csv('data/anscombe.csv')

print("Mean of x is %.3f" %(anascombe['x'].mean()))
print("Mean of x is %.3f" %(anascombe['y'].mean()))
print("Variance of x is %.3f" %(anascombe['x'].var()))
print("Variance of y is %.3f" %(anascombe['y'].var()))
print(anascombe[['x','y']].corr())

x=anascombe['x']
y=anascombe['y']
xx=sm.add_constant(x)
result=sm.OLS(y,xx).fit()
print(result.summary())

y_fit=model.fittedvalues
fig, fit=plt.subplots(figsize=(8, 6))
fit.plot(x,y,'o',label='anascombe')
fit.plot(x,y_fit,'r-')
Mean of x is 9.000
Mean of x is 7.501

Variance of x is 10.233
Variance of y is 3.837

          x         y
x  1.000000  0.816366
y  0.816366  1.000000

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.666
Model:                            OLS   Adj. R-squared:                  0.659
Method:                 Least Squares   F-statistic:                     83.92
Date:                Tue, 12 Jun 2018   Prob (F-statistic):           1.44e-11
Time:                        22:34:33   Log-Likelihood:                -67.358
No. Observations:                  44   AIC:                             138.7
Df Residuals:                      42   BIC:                             142.3
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const          3.0013      0.521      5.765      0.000       1.951       4.052
x              0.4999      0.055      9.161      0.000       0.390       0.610
==============================================================================
Omnibus:                        1.513   Durbin-Watson:                   2.327
Prob(Omnibus):                  0.469   Jarque-Bera (JB):                0.896
Skew:                           0.339   Prob(JB):                        0.639
Kurtosis:                       3.167   Cond. No.                         29.1
==============================================================================

拟合曲线如下

Part 2

Using Seaborn, visualize all four datasets.

hint: use sns.FacetGrid combined with plt.scatter

%matplotlib inline

import random

import numpy as np
import scipy as sp
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

import statsmodels.api as sm
import statsmodels.formula.api as smf

anascombe = pd.read_csv('data/anscombe.csv')

g = sns.FacetGrid(anscombe, col='dataset')
g = g.map(plt.scatter, 'x', 'y')

效果展示如下图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值