# (1),模型函数 def model(theta): h=np.dot(xx,theta) return h # (2),代价函数 def cost(h): j=1/(2*m)*((h-y).T.dot(h-y)) # j=0.5*np.mean((h-y)**2) return j # (3),梯度下降 grad def grad(num,alpha): theta=np.zeros(n) j=[] # 对模型进行循环处理,循环的过程就是对模型优化的过程 for i in range(num): h=model(theta) j.append(cost(h)) #append就是将所有的值依次存起来 deltea_theta=(1/m)*xx.T.dot(h-y) theta=theta-alpha*deltea_theta return theta,h,j
三个函数之间的关系,由数据充实模型,在模型没有完全成型的过程中,会被代价函数重新推送给算法继续完善