数据分析与机器学习之线性回归与逻辑回归(六) (5)

目标函数的构建
\[ h_\theta(x) = \theta x + \theta_0 预测函数 \]

#目标函数(损失函数) def cost(theta0,theta1,x,y): J=0 m = len(x) for i in range(m): h = theta1*x[i] + theta0 #对应公式 h(x)值 J += (h-y[i])**2 #目标函数 J = (h(x) - y)**2 J /= (2*m) return J print(cost(0,1,pga.distance,pga.accuracy)) #1.599438422599817 theta0 = 100 theta1s = np.linspace(-3,2,100) costs = [] for theta1 in theta1s: costs.append(cost(theta0,theta1,pga.distance,pga.accuracy)) print(theta1s.shape) #(100,) plt.plot(theta1s,costs) plt.show()

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspfjj.html