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]:
  dataset x y
0 I 10 8.04
1 I 8 6.95
2 I 13 7.58
3 I 9 8.81
4 I 11 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+ϵ (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() 
Jupyter作业_第1张图片

你可能感兴趣的:(Jupyter作业)