Jupyter作业

1.
In [1]:
%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.

In [4]:
anascombe = pd.read_csv('data/anscombe.csv')
anascombe.head()
Out[4]:
 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+ϵ (hint: use statsmodels and look at the Statsmodels notebook)
In [5]:
# your code here

Part 2

Using Seaborn, visualize all four datasets.

hint: use sns.FacetGrid combined with plt.scatter

代码:

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
import statistics as sta   
import scipy.stats.stats as stats 


anscombe = sns.load_dataset("anscombe")  
a = anscombe.x[:10].values    
b = anscombe.x[11:21].values  
c = anscombe.x[22:32].values  
d = anscombe.x[33:43].values 

a1 = np.mean(a)         
print("The mean of x in I: ", a1)  
b1 = np.mean(b)  
print("The mean of x in II: ", b1)  
c1 = np.mean(c)  
print("mean of x in III: ", c1)  
d1= np.mean(d)  
print("mean of x in IV: ", d1) 

a2=sta.variance(a)
print("The variance of x in I: ", a2)
b2=sta.variance(b)
print("The variance of x in I: ", b2)
c2=sta.variance(c)
print("The variance of x in I: ", c2)
d2=sta.variance(d)
print("The variance of x in I: ", d2)

m = anscombe.y[:10].values   
n = anscombe.y[11:21].values  
p = anscombe.y[22:32].values  
q = anscombe.y[33:43].values 

m1 = np.mean(m)         
print("The mean of x in I: ", m1)  
n1 = np.mean(n)  
print("The mean of x in II: ", n1)  
p1 = np.mean(p)  
print("mean of x in III: ", p1)  
q1= np.mean(q)  
print("mean of x in IV: ", q1) 

m2=sta.variance(m)
print("The variance of x in I: ", m2)
n2=sta.variance(n)
print("The variance of x in I: ", n2)
p2=sta.variance(p)
print("The variance of x in I: ", p2)
q2=sta.variance(q)
print("The variance of x in I: ", q2)

cof_I = stats.pearsonr(a, m)[0]   
cof_II = stats.pearsonr(b, n)[0]  
cof_III = stats.pearsonr(c, p)[0]  
cof_IV = stats.pearsonr(d, q)[0]  
print("correlation coefficient of I: ", cof_I)  
print("correlation coefficient of II: ", cof_II)  
print("correlation coefficient of III: ", cof_III)  
print("correlation coefficient of IV: ", cof_IV)

X_I = sm.add_constant(a)       
model_I = sm.OLS(m, X_I)  
result_I = model_I.fit()  
params_I = result_I.params  
print("DatasetI: y =", params_I[0], "+", params_I[1], "* x")  
  
X_II = sm.add_constant(b)  
model_II = sm.OLS(n, X_II)  
result_II = model_II.fit()  
params_II = result_II.params  
print("DatasetII: y =", params_II[0], "+", params_II[1], "* x")  
  
X_III = sm.add_constant(c)  
model_III = sm.OLS(p, X_III)  
result_III = model_III.fit()  
params_III = result_III.params  
print("DatasetIII: y =", params_III[0], "+", params_III[1], "* x")  
  
X_IV = sm.add_constant(d)  
model_IV = sm.OLS(q, X_IV)  
result_IV = model_IV.fit()  
params_IV = result_IV.params  
print("DatasetIV: y =", params_IV[0], "+", params_IV[1], "* x")  
  
sns.set(style='whitegrid')       
g = sns.FacetGrid(anscombe, col="dataset", hue="dataset", size=3)  
g.map(plt.scatter, 'x', 'y')  
plt.show() 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值