首页 > 解决方案 > 线性回归 python

问题描述

我想在没有 scikit-learn 的情况下在 python 中实现线性回归,但我不知道为什么,变量“错误”总是增加而且不应该,我认为问题可能出在公式中,但我不知道在哪里。

算法好吗?

def linear_regression(X, y, learning_rate=0.01, max_num_iterations=500, opt=True):
    # Your code goes here
    # Your code goes here
    w = np.zeros(len(X[0]))
    error=0
    gradient=X
    variable =X
    print(f"X:{X}")

    for a in range(max_num_iterations):

                o = np.sum(X*w)
                error = y-o
                print(f"sum1:{np.sum(X*w)}")
                print(f"y:{y}")
                print(f"error: {error}")
                print (f"gradient1: {gradient}")
                print(f"w: {w}")
                for i in range(800):

                    gradient[i][0]=-X[i][0]*error[i]

                    gradient[i][1]=-X[i][1]*error[i]

                gradient=-learning_rate*gradient

                w=w+gradient


    return w

标签: pythonlinear-regression

解决方案


推荐阅读