关键词:
梯度下降:就是让数据顺着梯度最大的方向,也就是函数导数最大的放下下降,使其快速的接近结果。
Cost函数等公式太长,不在这打了。网上多得是。
这个非线性回归说白了就是缩小版的神经网络。
python实现:
1 importnumpy as np2 importrandom3
4 def graientDescent(x,y,theta,alpha,m,numIterations):#梯度下降算法
5 xTrain =x.transpose()6 for i in range(0,numIterations):#重复多少次
7 hypothesis=np.dot(x,theta) #h函数
8 loss=hypothesis-y9
10 cost=np.sum(loss**2) / (2*m)11 print("Iteration %d / cost:%f"%(i,cost))12 graient=np.dot(xTrain,loss)/m13 theta=theta-alpha*graient14 returntheta15
16 def getData(numPoints,bias,variance):#自己生成待处理数据
17 x=np.zeros(shape=(numPoints,2))18 y=np.zeros(shape=numPoints)19 for i inrange(0,numPoints):20 x[i][0]=1
21 x[i][1] =i22 y[i]=(i+bias)+random.uniform(0,1)*variance23 returnx,y24
25 X,Y=getData(100,