利用梯度下降 Gradient Descent 求解线性回归

代码示例来自 Udacity 课程中 Siraj 的一个教学视频,版权归属于原作者及 Udacity 所有,源代码及数据源可见 Siraj 的 Github,尽管代码可以直接下载,我还是选择跟随视频手动完成的方式,也对于引用方式和变量命名做了一点修改,放在这里方便随时查看。

这个代码示例主要是为了演示梯度下降的实现过程,用它来求解线性回归稍微有些大材小用,但不妨碍说明问题。

import numpy as np

def compute_error_for_points(b, m, points):
    total_error = 0
    for i in range(len(points)):
        x = points[i, 0]
        y = points[i, 1] 
        total_error += (y - (m * x + b)) ** 2
    return total_error / float(len(points))

def step_gradient(b_current, m_current, points, learning_rate):
    # core gradient descent computation
    b_gradient = 0
    m_gradient = 0
    N = float(len(points))
    for i in range(len(points)):
        x = points[i, 0]
        y = points[i, 1]
        b_gradient += -(2 / N) * (y - (m_current * x + b_current))
        m_gradient += -(2 / N) * x * (y - (m_current * x + b_current))
    new_b = b_current - learning_rate * b_gradient
    new_m = m_current - learning_rate * m_gradient
    return [new_b, new_m]

def gradient_descent_runner(points, starting_b, starting_m, learning_rate, num_iterations):
    b = starting_b
    m = starting_m

    for i in range(num_iterations):
        b, m = step_gradient(b, m, np.array(points), learning_rate) 
    return [b, m]

def run():
    points = np.genfromtxt('data.csv', delimiter=',') 

    # hyperparameters
    learning_rate = 0.0001

    # y = mx + b
    initial_b = 0
    initial_m = 0
    num_iterations = 1000
    [b, m] = gradient_descent_runner(points, initial_b, initial_m, learning_rate, num_iterations)
    print(b)
    print(m)


if __name__ == '__main__':
    run()

你可能感兴趣的:(利用梯度下降 Gradient Descent 求解线性回归)