作业网址:
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
===================================================================&