Anscombe's quartet-homework

这篇博客探讨了Anscombe's quartet概念,并通过Python进行数据分析预处理,展示了如何在作业中应用这些概念。
摘要由CSDN通过智能技术生成

作业网址:

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

预处理代码以及头文件:

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('data/anscombe.csv')
anascombe.head()

# part one
print("mean of both x and y:")
print('mean of x:',anascombe.groupby('dataset')['x'].mean())  
print('mean of y:',anascombe.groupby('dataset')['y'].mean())  

print("variance of both x and y:") 
print('variance of x:',anascombe.groupby('dataset')['x'].var())  
print('variance of y:',anascombe.groupby('dataset')['y'].var())  
 
corr_mat = anascombe.groupby('dataset').corr() 
print('correlation coefficient:')  
print('I:',corr_mat['x']['I']['y'])  
print('II:',corr_mat['x']['II']['y'])  
print('III:',corr_mat['x']['III']['y'])  
print('IV:',corr_mat['x']['IV']['y'])  

print('the linear regression:')  
for key in anascombe.groupby('dataset').indices:  
    group = anascombe.groupby('dataset').get_group(key)  
    n = len(group)  
    is_train = np.random.rand(n)>-np.inf  
    train = group[is_train].reset_index(drop=True)  
    lin_model = smf.ols('y ~ x', train).fit()  
    print('dataset '+str(key)+':')  
    print(lin_model.summary())  
输出结果:
mean of both x and y:
mean of x: dataset
I      9.0
II     9.0
III    9.0
IV     9.0
Name: x, dtype: float64
mean of y: dataset
I      7.500909
II     7.500909
III    7.500000
IV     7.500909
Name: y, dtype: float64
variance of both x and y:
variance of x: dataset
I      11.0
II     11.0
III    11.0
IV     11.0
Name: x, dtype: float64
variance of y: dataset
I      4.127269
II     4.127629
III    4.122620
IV     4.123249
Name: y, dtype: float64
correlation coefficient:
I: 0.81642051634484
II: 0.8162365060002428
III: 0.8162867394895981
IV: 0.8165214368885028
the linear regression:
dataset I:
C:\Users\MECHREV\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\stats\stats.py:1394: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=11
  "anyway, n=%i" % int(n))
                            OLS Regression Results                            
===================================================================&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值