我正在尝试编写一个代码,使用梯度下降返回岭回归的参数。岭回归定义为
\n\n其中,L 是损失(或成本)函数。w 是损失函数的参数(同化 b)。x 是数据点。y 是每个向量 x 的标签。lambda 是正则化常数。b 是截距参数(同化为 w)。所以,L(w,b) = 数字
\n我应该实现的梯度下降算法如下所示:
\n\n其中\xe2\x88\x87\是L相对于w的梯度。\xce\xb7
\n是步长。t 是时间或迭代计数器。
\n\n我的代码:
\ndef ridge_regression_GD(x,y,C):\n x=np.insert(x,0,1,axis=1) # adding a feature 1 to x at beggining nxd+1\n w=np.zeros(len(x[0,:])) # d+1\n t=0\n eta=1\n summ = np.zeros(1)\n grad = np.zeros(1)\n losses = np.array([0])\n loss_stry = 0\n while eta > 2**-30:\n for i in range(0,len(y)): # here we calculate the summation for all rows for loss and gradient\n summ=summ+((y[i,]-np.dot(w,x[i,]))*x[i,])\n …Run Code Online (Sandbox Code Playgroud)