import numpy as np
import statsmodels.formula.api as smf
import pylab as plt
a = np.loadtxt('F:\python数学建模与算法\源程序\《Python数学建模算法与应用》程序和数据//10第10章 回归分析/data10_4.txt'); x1 = a[0]; x2 = a[1]; y = a[2]
plt.rc('axes', unicode_minus=False); plt.rc('font', family='SimHei')
plt.plot(x1, y, '', label='$x_1$'); plt.plot(x2, y, 'o', label='$x_2$')
d = {'y': y, 'x1': x1, 'x2': x2}
re1 = smf.ols('y~x1+x2', d).fit()
print('线性回归的残差方差:', re1.mse_resid)
re2 = smf.ols('y~x1+x2+I(x12)+I(x22)', d).fit()
print('纯二次的残差方差:', re2.mse_resid)
re3 = smf.ols('y~x1x2', d).fit()
print('交叉的残差方差:', re3.mse_resid)
re4 = smf.ols('y~x1*x2+I(x12)+I(x22)', d).fit()
print('完全二次的残差方差:', re4.mse_resid)
print('预测值:', re2.predict({'x1': 170, 'x2': 160}))
print(re2.summary()); plt.legend(); plt.show()