在Python中,正则化的线性回归有什么不对?

2024-06-26 00:10:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我用numpy编写代码(theta,X是numpy数组):

def CostRegFunction(X, y, theta, lambda_):
    m = len(X)
    # add bias unit
    X = np.concatenate((np.ones((m,1)),X),1)

    H = np.dot(X,theta)

    J = (1 / (2 * m)) * (np.sum([(H[i] - y[i][0])**2 for i in range(len(H))])) + (lambda_ / (2 * m)) * np.sum(theta[1:]**2)

    grad_ = list()

    grad_.append((1 / m) * np.sum([(H[j] - y[j][0]) for j in range(len(H))]))
    for i in range(len(theta)-1):
        grad_.append((1 / m) * np.sum([(H[j] - y[j]) * X[j][i+1] for j in range(len(H))]) + (lambda_ / m) * theta[i+1])

    return J, grad_



def TrainLinearReg(X, y, theta, lambda_, alpha, iter):

    JHistory = list()
    for i in range(iter):
        J, grad = CostRegFunction(X, y, theta, Lambda_)
        JHistory.append(J)
        for j in range(len(theta)):
            theta[j] = theta[j] - alpha * grad[j]

    return theta, JHistory

Theta, JH = TrainLinearReg(X, y, th, Lambda_, 0.01, 50)

但当我学习θ时,这段代码给了我θ和J值的巨大增长。 例如,first iteration grad=[-15.12452,598.435436]——它是正确的。J是303.3255 第二次迭代-grad=[10.23566,-3646.2345]J=7924 依此类推,J增长得越来越快,但在LR的概念下,它必须更低。在

但是如果我用普通的线性方程,会得到一个很好的θ。在

那个密码有什么问题?在


Tags: lambda代码innumpyforlendefnp
1条回答
网友
1楼 · 发布于 2024-06-26 00:10:58

numpy版本

一。10月17日编辑

我用numpy库重写了部分代码。在

所有向量现在都是列numpy数组。在

import numpy as np
from copy import deepcopy as dc
from matplotlib import pyplot as plt

_norm = np.linalg.norm    

def CostRegFunction(X, y, theta, lambda_):
    m = len(X)
    H = np.dot(X,theta)
    J = (1 / (2 * m)) * _norm(H-y)**2 + (lambda_ / (2 * m)) * _normal(theta[1:])**2
    grad_ = np.array(sum(H-y)/m,ndmin=2).T
    for i in range(theta.shape[0]-1):
        grad_=np.concatenate((grad_,np.array(sum((H-y)*np.array(X[:,1],ndmin=2).T)/m + (lambda_/m) * theta[i+1],ndmin=2).T),0)
    return J, grad_

def TrainLinearReg(X, y, theta, lambda_, alpha, iter):
    JHistory = list()
    # add bias unit -> it's better to do it here, before entering the loop
    X = np.concatenate((np.ones((X.shape[0],1)),X),1)
    for i in range(iter):
        J, grad = CostRegFunction(X, y, theta, lambda_)
        JHistory.append(J)
        theta = theta -  alpha*grad
    return theta, JHistory

然后我生成了一组带白噪声的x-y多项式数据,并使用TrainLinearReg函数拟合多项式方程。在

^{pr2}$

我得到的是以下信息。在

plt.plot(x[:,0],y,'o',label='Original Data',alpha = 0.5)
x2 = np.linspace(0,10,10)
plt.plot(x2,Theta[0]+x2*Theta[1]+x2**2*Theta[2],'-',label='Fitted     Curve',lw=1.5,alpha=0.8,color='black')
plt.gca().set_xlabel('x')
plt.gca().set_ylabel('y')
plt.legend()

Output >> Theta = array([[ 1.29259285],
                         [-2.97763304],
                         [-1.98758321]])

enter image description here

我希望我能帮上忙。在

谨致问候, 加布里埃尔

相关问题 更多 >