pandas 习题

题目来源:

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


Anscombe's quartet

Anscombe's quartet comprises of four datasets, and is rather famous. Why? You'll find out in this exercise.

初始数据:


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+ϵy=β0+β1x+ϵ (hint: use statsmodels and look at the Statsmodels notebook

结果:

means:  

variance:   

correlation coefficient: 

model:  





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

sns.set_context("talk")

#读取并显示初始数据
anascombe = pd.read_csv('Anscombe.csv')
data = anascombe.head()
print(data)
#计算并显示平均数
means = anascombe.groupby('dataset')['x','y'].mean()    
print("the mean of x and y:")
print(means)
#计算并显示方差
std = anascombe.groupby('dataset')['x','y'].std()
print("the variance of x and y:")
print(std)
#计算并显示相关系数
corr = anascombe.groupby('dataset')['x','y'].corr()
print("the correlation coefficient of x and y:")
print(corr)

print()
#拟合并输出结果
l = ['I','II','III','IV']
for i in l:
    x = anascombe[anascombe['dataset'] == i]['x']
    y = anascombe[anascombe['dataset'] == i]['y']
    #增加常数项
    x = sm.add_constant(x)
    model = sm.OLS(y,x).fit()
    print('the model of data '+i+' :')
    print(model.params)
    print(model.summary())
    
g = sns.FacetGrid(anascombe, col="dataset")
g.map(plt.scatter, "x", "y")
plt.show()



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值