高级编程技术第十四周作业:jupyter练习

#首先下载安装好jupyter以及所需的模块文件statsmodels等
#导入所需的模块文件
%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.

#读取数据集
ana = pd.read_csv('data/anscombe.csv')
ana.head()
  dataset x y
0 I 10.0 8.04
1 I 8.0 6.95
2 I 13.0 7.58
3 I 9.0 8.81
4 I 11.0 8.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+β1x+ϵ (hint: use statsmodels and look at the Statsmodels notebook)
print("Question1:计算均值和方差")
print("Mean of x is %.1f and variance of x is %.1f"% (ana['x'].mean(),ana['x'].var()))
print("Mean of y is %.1f and variance of y is %.1f"% (ana['y'].mean(),ana['y'].var()))
print("\nQuestion2:计算相关系数")
print(ana[['x','y']].corr())#使用pandas内置函数corr
print("\nQuestion3:计算线性回归方程")

#自变量和因变量
x = ana['x']
y = ana['y']

#为模型增加常数项
x = sm.add_constant(x)
#使用OLS对象的fit()方法来进行模型拟合
outc = sm.OLS(y,x)
outc = outc.fit()

print(outc.summary())
print("拟合模型的参数为")
print(outc.params)

输出结果:

Question1:计算均值和方差
Mean of x is 9.0 and variance of x is 10.2
Mean of y is 7.5 and variance of y is 3.8

Question2:计算相关系数
          x         y
x  1.000000  0.816366
y  0.816366  1.000000

Question3:计算线性回归方程
                            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:                Mon, 11 Jun 2018   Prob (F-statistic):           1.44e-11
Time:                        17:32:27   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
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
拟合模型的参数为
const    3.001295(常量)
x        0.499932(自变量系数)
dtype: float64

Part 2

Using Seaborn, visualize all four datasets.

hint: use sns.FacetGrid combined with plt.scatter


#直接调用sns.FacetGrid对四个数据集进行可视化即可
p = sns.FacetGrid(ana,col = "dataset", hue = "dataset")
p.map(plt.scatter,'x','y')
输出结果:

高级编程技术第十四周作业:jupyter练习_第1张图片

你可能感兴趣的:(高级编程技术第十四周作业:jupyter练习)