高级编程技术第十四周作业: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()
 datasetxy
0I10.08.04
1I8.06.95
2I13.07.58
3I9.08.81
4I11.08.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')
输出结果:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值