Anscombe's quartet 学习

本文档介绍了如何使用Ipython、Jupyter、pandas、seaborn和statsmodels来研究Anscombe's quartet。首先,进行了必要的准备工作,包括安装库和配置Python环境。接着,在Part 1中,分别计算了四个数据集的x和y的平均值、方差、相关系数以及线性回归线。然后,在Part 2中,利用Seaborn的FacetGrid和plt.scatter方法,可视化展示了所有四个数据集。
摘要由CSDN通过智能技术生成

题目网址:

https://nbviewer.jupyter.org/github/schmit/cme193-ipython-notebooks-lecture/blob/master/Exercises.ipynb

准备工作

需安装的文件: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:&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值