前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >用Numpy实现线性回归

用Numpy实现线性回归

作者头像
mathor
发布2019-12-30 16:00:51
发布2019-12-30 16:00:51
1.9K00
代码可运行
举报
文章被收录于专栏:mathormathor
运行总次数:0
代码可运行

用Numpy实现线性回归

现在二维平面上有一系列点point,我们要找到一个一次函数$y=wx+b$,使得所有点到这条直线的距离平方和$\sum(wx+b-y)^2$最小

因此我们可以定义损失函数$loss = (wx+b-y)^2$,计算损失的代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
# compute loss
def compute_error_for_line_given_points(b, w, points):
    totalError = 0
    for i in range(len(points)):
        x = points[i, 0]
        y = points[i, 1]
        totalError += (y - (w * x + b)) ** 2
    return totalError / float(len(points)) # average

然后用梯度下降法更新$w$和$b$

$w' = w - lr*\frac{\partial loss}{\partial w}$,$b' = b - lr*\frac{\partial loss}{\partial b}$,其中 $\frac{\partial loss}{\partial w} = 2 * x * (wx + b - y)$,$\frac{\partial loss}{\partial b} = 2 * (wx + b - y)$

代码语言:javascript
代码运行次数:0
运行
复制
# compute gradient
def step_gradient(b_current, w_current, points, learningRate):
    b_gradient = 0
    w_gradient = 0
    N = float(len(points))
    for i in range(len(points)):
        x = points[i, 0]
        y = points[i, 1]
        b_gradient += 2 * ((w_current * x) + b_current - y)
        w_gradient += 2 * x * ((w_current * x) + b_current - y)
    b_gradient = b_gradient / N
    w_gradient = w_gradient / N
    new_b = b_current - (learningRate * b_gradient)
    new_w = w_current - (learningRate * w_gradient)
    return [new_b, new_w]

最后只要设定迭代次数,不断的重复更新$w$和$b$就行了

代码语言:javascript
代码运行次数:0
运行
复制
def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iterations): # num_iteration 迭代次数
    b = starting_b
    w = starting_w
    for i in range(num_iterations):
        b, w = step_gradient(b, w, np.array(points), learning_rate)
    return [b, w]

主函数

代码语言:javascript
代码运行次数:0
运行
复制
def run():
    points = np.genfromtxt("data.txt", delimiter=",")
    learning_rate = 0.0001
    initial_b = random()
    initial_w = random()
    num_iterations = 1000
    print("Starting gradient descent at b = {0}, w = {1}, error = {2}"
          .format(initial_b, initial_w, 
                  compute_error_for_line_given_points(initial_b, initial_w, points)))
    print("Running...")
    [b, w] = gradient_descent_runner(points, initial_b, initial_w, learning_rate, num_iterations)
    print("After {0} iterations at b = {1}, w = {2}, error = {3}"
          .format(num_iterations, b, w, 
                  compute_error_for_line_given_points(b, w, points)))
run()

data.txt数据文件下载

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 用Numpy实现线性回归
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档