Anscombe's quartet-homework

这篇博客探讨了Anscombe's quartet概念,并通过Python进行数据分析预处理,展示了如何在作业中应用这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作业网址:

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                            
===================================================================&
R语言是一种广泛用于统计分析和图形展示的编程语言。要绘制Anscombe四个数据集的散点图,你可以按照以下步骤操作: 1. 首先,你需要安装并加载必要的包,如`ggplot2`,它是R中创建复杂图形的主要工具之一。如果还没有安装,可以使用`install.packages("ggplot2")`命令安装。 2. 然后,加载`ggplot2`和其他可能需要的库,例如`tidyverse`(包括一系列常用的数据处理和绘图函数): ```R library(ggplot2) library(tidyverse) ``` 3. Anscombe的数据集已经内置在R中,可以通过`data()`函数直接获取,然后查看前几行确认数据: ```R data(anscombe) head(anscombe) ``` 4. 接下来,你可以选择一个数据集(比如第1组、第2组等),将其转化为适合`ggplot2`的格式,通常通过`gather()`函数将数据展开: ```R anscombe_long <- gather(anscombe, key = "variable", value = "value", everything()) ``` 5. 最后,使用`ggplot()`开始绘制图形,设置x轴和y轴变量,加上散点图层,并添加标题描述每个数据集: ```R anscombe_plots <- anscombe_long %>% ggplot(aes(x = value, y = variable)) + geom_point() + facet_wrap(~ variable, ncol = 2) + # 分为两列显示四个数据集 labs(title = "Anscombe's Quartet: Four Similar-looking Datasets") + theme_bw() # 使用黑白色调 print(anscombe_plots) ``` 完成以上步骤后,你将看到Anscombe四个数据集的散点图,尽管它们看起来非常相似,但仔细观察会发现一些微妙的区别。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值