Anscombe's quartet

本博客通过计算四个不同数据集的均值、方差及相关系数,并利用Seaborn进行可视化,展示了尽管统计数据相似,但数据分布却可能截然不同的现象。

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

题目描述

%matplotlib inline

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")
Anscombe’s quartet

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

anascombe = pd.read_csv('data/anscombe.csv')
anascombe.head()
datasetxy
0108.04
186.95
2137.58
398.81
4118.33
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)
Part 2

Using Seaborn, visualize all four datasets.

hint: use sns.FacetGrid combined with plt.scatter


解题代码

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import statsmodels.api as sm
import statistics as sta
import scipy.stats.stats as stats

anscombe = sns.load_dataset("anscombe")
print(anscombe)  # 打印原数据

str = ['I', 'II', 'III', 'IV']
Xarray = []
Yarray = []

for i in range(0, 4):
    array = anscombe.x[i * 11:i * 11 + 10].values   # 获取x的值,并打印
    Xarray.append(array)
    print("Xarray in " + str[i] + ":", Xarray[i])
    array = anscombe.y[i * 11:i * 11 + 10].values  # 获取x的值,并打印
    Yarray.append(array)
    print("Yarray in " + str[i] + ":", Yarray[i])

for i in range(0, 4):
    Xmean = np.mean(Xarray[i])   # 计算x的平均值,并打印
    print("mean of x in " + str[i] + ":", Xmean)
    Xvariance = sta.variance(Xarray[i])    # 计算x的方差,并打印
    print("variance of x in " + str[i] + ":", Xvariance)

print(' ')

for i in range(0, 4):
    Ymean = np.mean(Yarray[i])   # 计算y的平均值,并打印
    print("mean of x in " + str[i] + ":", Ymean)
    Yvariance = sta.variance(Yarray[i])    # 计算y的方差,并打印
    print("variance of x in " + str[i] + ":", Yvariance)

print('')

for i in range(0, 4):
    Cof = stats.pearsonr(Xarray[i], Yarray[i])[0]
    print("correlation coefficient of " + str[i] + ":", Cof)

print('')

for i in range(0, 4):
    X = sm.add_constant(Xarray[i])
    model = sm.OLS(Yarray[i], X)
    result = model.fit()
    params = result.params
    print("Dataset" + str[i] + ": y =", params[0], "+", params[1], "* x")

sns.set(style = 'whitegrid')  # 数据可视化,散点图
g = sns.FacetGrid(anscombe, col = "dataset", hue = "dataset", size = 3)
g.map(plt.scatter, 'x', 'y')
plt.show()


可视化结果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值