Jupyter练习题

Jupyter练习题_第1张图片

按照题目所给代码:

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 1:

  • Compute the mean and variance of both x and y

代码:

print(anascombe.groupby('dataset')['x','y'].mean())  
print(anascombe.groupby('dataset')['x','y'].var())

结果:

Jupyter练习题_第2张图片

  • Compute the correlation coefficient between x and y


代码:

print(anascombe.cov()['x']['y'] / ((anascombe['x'].var() * anascombe['y'].var()) ** 0.5))

结果:


  • Compute the linear regression line: y=β0+β1x+ϵy=β0+β1x+ϵ (hint: use statsmodels and look at the Statsmodels notebook)

代码:

linear_regression_line = smf.ols('y ~ x', anascombe).fit()  
print(linear_regression_line.summary())

结果:

Jupyter练习题_第3张图片

Part 2:

代码:

image = sns.FacetGrid(anascombe, col='dataset')  
image.map(plt.scatter, 'x', 'y')  
plt.show()

结果:

Jupyter练习题_第4张图片

你可能感兴趣的:(作业)