Python学习之路_jupyter notebook

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()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
datasetxy
0I10.08.04
1I8.06.95
2I13.07.58
3I9.08.81
4I11.08.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+ϵ y = β 0 + β 1 x + ϵ (hint: use statsmodels and look at the Statsmodels notebook)

def get_data(data):
    return pd.Series([data['x'].mean(), data['x'].var(), data['y'].mean(), data['y'].var()],index=['x均值', 'x方差', 'y均值', 'y方差'])

dataset_name = anascombe.dataset.unique()
group = anascombe.groupby(by=list(["dataset"]))
for name in dataset_name:
    data = group.get_group(name)
    print('dataset: ', name)
    print(pd.DataFrame(get_data(data)))
    print('系数')
    print(data.corr(),'\n')
    x = data['x']
    X = sm.add_constant(data['x'])
    y = data['y']
    model = sm.OLS(y,X)
    results = model.fit()
    print(results.params)
    y_fitted = results.fittedvalues
    fig, ax = plt.subplots()
    ax.plot(x, y, 'o', label='data')
    ax.plot(x, y_fitted, 'r-',label='OLS')
    ax.legend(loc='best')
    plt.show()
    print('\n')
dataset:  I
             0
x均值   9.000000
x方差  11.000000
y均值   7.500909
y方差   4.127269
系数
          x         y
x  1.000000  0.816421
y  0.816421  1.000000 

const    3.000091
x        0.500091
dtype: float64

这里写图片描述

dataset:  II
             0
x均值   9.000000
x方差  11.000000
y均值   7.500909
y方差   4.127629
系数
          x         y
x  1.000000  0.816237
y  0.816237  1.000000 

const    3.000909
x        0.500000
dtype: float64

png

dataset:  III
            0
x均值   9.00000
x方差  11.00000
y均值   7.50000
y方差   4.12262
系数
          x         y
x  1.000000  0.816287
y  0.816287  1.000000 

const    3.002455
x        0.499727
dtype: float64

这里写图片描述

dataset:  IV
             0
x均值   9.000000
x方差  11.000000
y均值   7.500909
y方差   4.123249
系数
          x         y
x  1.000000  0.816521
y  0.816521  1.000000 

const    3.001727
x        0.499909
dtype: float64

这里写图片描述

Part 2

Using Seaborn, visualize all four datasets.

hint: use sns.FacetGrid combined with plt.scatter

    g = sns.FacetGrid(anascombe, col="dataset", size=4)
    g = g.map(plt.scatter, "x", "y", edgecolor="w")

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值