参考链接:
1,基本原理:https://zhuanlan.zhihu.com/p/149280941
2,python实现:https://zhuanlan.zhihu.com/p/22692029
实现结果
线性回归:
# --coding:utf-8--
# 简单线性回归demo
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
sample_num=100
x=np.linspace(0,10,sample_num)
X=sm.add_constant(x)
beta = np.array([1,100]) # beta0,beta1的真值分别为1,10
e = np.random.normal(size=sample_num) #0-1正太分布误差项
y=np.dot(X,beta) + e # 反应项
model = sm.OLS(y,X) #送入OLS库中进行建模
result = model.fit()#输出结果
print(result.params)
print(result.summary())
# 调用回归系数
y_fitted = result.fittedvalues
fig,ax = plt.subplots(figsize=(8,6))
ax.plot(x,y,'or',label='ori')
ax.plot(x,y_fitted,'-b',label='fitted')
ax.legend(loc='best')
plt.show()
非线性回归
# --coding:utf-8--
# 简单高次模型回归
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
# 拟合高次x,y关系,有一个条件是需要必须知道这个关系中所有次方的次数。
# 本次demo中假设 y=1+0.1x+10x^2,已知 x,x,x^2,求拟合1,0.1,10
sampleNum=100
x=np.linspace(0,10,sampleNum) #x依然取值0-10,100个
X=np.column_stack((x,x**2))
X=sm.add_constant(X) #构成三列[1,x,x^2] X
e=np.random.normal(size=sampleNum)
beta = np.array([2,0.3,20])
Y=np.dot(X,beta) + e #真实值Y
model=sm.OLS(Y,X)
results=model.fit()
print(results.params)
print(results.summary())
Y_fited=results.fittedvalues
fig,ax=plt.subplots(figsize=(8,6))
ax.plot(x,Y,'or',label='ori')
ax.plot(x,Y_fited,'-b',label='fitted')
ax.legend(loc='best')
plt.show()