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: Pandas 练习题 [python] (hint: use statsmodels and look at the Statsmodels notebook)
In [5]:
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('anscombe.csv')  
datasets = ['I', 'II', 'III', 'IV']  


for dataset in datasets:
    data = anascombe[anascombe['dataset']==dataset]
    print(dataset + ':')
    print("mean of x:",data.x.mean()," mean of y:",data.y.mean())
    print("variance of x:",data.x.var()," variance of y:",data.y.var())
print("")


print("correlation coefficient between x and y:",anascombe.x.corr(anascombe.y))
print("")




x = anascombe['x'].values  
y = anascombe['y'].values 
x = sm.add_constant(x)  
ols = sm.OLS(y, x)  
result = ols.fit()
print("Linear regression line: B1, B2")
print(result.params)  
Out [5]:

Pandas 练习题 [python]

Part 2

Using Seaborn, visualize all four datasets.

hint: use sns.FacetGrid combined with plt.scatter

In [6]:
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('anscombe.csv')
gra = sns.FacetGrid(anascombe, col = 'dataset')
gra = gra.map(plt.scatter, "x", "y")
plt.show()

 Out [6]:

Pandas 练习题 [python]

相关文章: