参考公式
公式参考文献:https://blog.csdn.net/i96jie/article/details/81252198
具体分析参考之前的梯度下降代码:
import matplotlib.pyplot as plt import numpy as np x=[[400],[450],[484],[500],[510],[525],[540],[549],[558],[590],[610],[640],[680],[750],[900]] y=[[80],[89],[92],[102],[121],[160],[180],[189],[199],[203],[247],[250],[259],[289],[356]] x2=[] plt.plot(x,y,'ks') for i in range(len(x)): x2.append(x[i][0]*x[i][0]) def polynomial_regression(x,x2,y): alpha=0.00000000001 m=len(x) w0=0 w1=0 w2=0 count=0 error2=0 while 1: finish=0 w=[0,0,0] for i in range(m): count+=1 w[0]=w0+w1*x[i][0]+w2*x2[i]-y[i][0] w[1]=(w0+w1*x[i][0]+w2*x2[i]-y[i][0])*x[i][0] w[2]=(w0+w1*x[i][0]+w2*x2[i]-y[i][0])*x2[i] w0=w0-alpha*w[0] w1=w1-alpha*w[1] w2=w2-alpha*w[2] error1=abs(w0+w1*x[i][0]+w2*x2[i]-y[i][0]) if abs(error2-error1)<1:#收敛程度 finish=1 break if(count>15): finish=1 break error2=error1 print('w0:%f,w1:%f,w2:%f,error:%f'%(w0,w1,w2,error1)) if(finish==1): break y=[] for i in range(len(x)): y.append(w0+w1*x[i][0]+w2*x2[i]) plt.plot(x,y,'g') polynomial_regression(x,x2,y) plt.show()
运行结果: