原题目网址:https://nbviewer.jupyter.org/github/schmit/cme193-ipython-notebooks-lecture/blob/master/Exercises.ipynb
第一题:
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 +β 1 x+ϵ y=β0+β1x+ϵ (hint: use statsmodels and look at the Statsmodels notebook
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")
x =pd.read_csv('anscombe.csv').x
y =pd.read_csv('anscombe.csv').y
print('Mean of x:', str(x.mean()))
print('Variance of x:', str(x.var()))
print('Mean of y:', str(y.mean()))
print('Variance of y:', str(y.var()))
coefficient = x.corr(y, method='pearson', )
print('the correlation coefficient between x and y:', str(coefficient))
res = smf.ols('y~x', data=x.add(y)).fit()
print(res.summary())
第二题:
Using Seaborn, visualize all four datasets.
hint: use sns.FacetGrid combined with plt.scatter
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")
anascombe = pd.read_csv('anscombe.csv')
tmp= sns.FacetGrid(anascombe, col="dataset").map(plt.scatter, "x", "y", color="r")
plt.show()