题目网址:
准备工作
需安装的文件:Ipython、Jupyter
要配置的Python的库:pandas、seaborn、statsmodels
使用pip进行安装
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)
1.1 code:
#1.1
variable = 'xy'
print("mean:")
for var in variable:
print(anascombe.groupby("dataset")[var].mean())
print("var:")
for var in variable:
print(anascombe.groupby("dataset")[var].var())
1.1 result:
mean:
dataset
I 9.0
II 9.0
III 9.0
IV 9.0
Name: x, dtype: float64
dataset
I 7.500909
II 7.500909
III 7.500000
IV 7.500909
Name: y, dtype: float64
var:
dataset
I 11.0
II 11.0
III 11.0
IV 11.0
Name: x, dtype: float64
dataset
I 4.127269
II 4.127629
III 4.122620
IV 4.123249
Name: y, dtype: float64
[Finished in 11.2s]
1.2 code:
# 1.2
corrs = anascombe.groupby("dataset").corr()
print("\n1.2\ncorrelation coefficient between x and y")
for key in anascombe.groupby('dataset').indices:
print("%-6s" % key, corrs['x'][key]['y'])
1.2 result:
1.2
correlation coefficient between x and y
I 0.816420516345
II 0.816236506
III 0.81628673949
IV 0.816521436889
[Finished in 9.5s]
1.3 code
print('\n1.3\nlinear regression:&#