机器学习(三)--------多变量线性回归(Linear Regression with Multiple Variables)

同样是预测房价问题  如果有多个特征值

那么这种情况下  假设h表示为 

公式可以简化为

两个矩阵相乘   其实就是所有参数和变量相乘再相加  所以矩阵的乘法才会是那样

那么他的代价函数就是

同样是寻找使J最小的一系列参数

python代码为

比如这种     那么X是[1,2,3]   y也是[1,2,3]   那么令theta0 = 0  theta1 = 1   这个函数返回值为0最小      theta0 = 0 theta1=0的话  返回值是2.333

要考虑是否需要特征缩放,特征缩放就是特征分配不均时   会导致梯度下降耗费更多  为了让梯度下降更快

所以

如何选择学习率α呢

梯度下降算法的每次迭代受到学习率的影响,如果学习率 过小,则达到收敛所需的迭代次数会非常高,如果学习率过大,每次迭代可能不会减小代价函数,可能会越过局部最小值导致无法收敛。

通常可以考虑尝试些学习率:0.01,0.03,0.3,1,3,10

而有的时候线性回归并不适用于所有的模型,这个时候我们要考虑用多项式模型

这个时候特征缩放就很重要

梯度下降  线性回归的python代码

# -*- coding=utf8 -*-

import math;

def sum_of_gradient(x, y, thetas):
"""计算梯度向量,参数分别是x和y轴点坐标数据以及方程参数"""
m = len(x);
grad0 = 1.0 / m * sum([(thetas[0] + thetas[1] * x[i] - y[i]) for i in range(m)])
grad1 = 1.0 / m * sum([(thetas[0] + thetas[1] * x[i] - y[i]) * x[i] for i in range(m)])
return [grad0, grad1];

def step(thetas, direction, step_size):
"""move step_size in the direction from thetas"""
return [thetas_i + step_size * direction_i
for thetas_i, direction_i in zip(thetas, direction)]

def distance(v, w):
"""两点的距离"""
return math.sqrt(squared_distance(v, w))

def squared_distance(v, w):
vector_subtract = [v_i - w_i for v_i, w_i in zip(v, w)]
return sum(vector_subtract_i * vector_subtract_i for vector_subtract_i, vector_subtract_i
in zip(vector_subtract, vector_subtract))

def gradient_descent(stepSize, x, y, tolerance=0.000000001, max_iter=100000):
"""梯度下降"""
iter = 0
# initial theta
thetas = [0, 0];
# Iterate Loop
while True:
gradient = sum_of_gradient(x, y, thetas);

next_thetas = step(thetas, gradient, stepSize);

if distance(next_thetas, thetas) < tolerance: # stop if we're converging
break
thetas = next_thetas # continue if we're not

iter += 1 # update iter

if iter == max_iter:
print 'Max iteractions exceeded!'
break;

return thetas

x = [1, 2, 3];
y = [5, 9, 13];
stepSize = 0.001;
t0, t1 = gradient_descent(-stepSize, x, y);
print t0, " ", t1;

线性回归还有一种更简单的  就是正规方程

这个是用数学推导出来的

两者对比: 

机器学习(三)--------多变量线性回归(Linear Regression with Multiple Variables)的更多相关文章

  1. 机器学习 (二) 多变量线性回归 Linear Regression with Multiple Variables

    文章内容均来自斯坦福大学的Andrew Ng教授讲解的Machine Learning课程,本文是针对该课程的个人学习笔记,如有疏漏,请以原课程所讲述内容为准.感谢博主Rachel Zhang 的个人 ...

  2. 机器学习第4课:多变量线性回归(Linear Regression with Multiple Variables)

    4.1  多维特征 目前为止,我们探讨了单变量/特征的回归模型,现在我们对房价模型增加更多的特征, 例如房间数楼层等,构成一个含有多个变量的模型,模型中的特征为(x1,x2,...,xn).

  3. 斯坦福第四课:多变量线性回归(Linear Regression with Multiple Variables)

    4.1  多维特征 4.2  多变量梯度下降 4.3  梯度下降法实践 1-特征缩放 4.4  梯度下降法实践 2-学习率 4.5  特征和多项式回归 4.6  正规方程 4.7  正规方程及不可逆性 ...

  4. Ng第四课:多变量线性回归(Linear Regression with Multiple Variables)

    4.1  多维特征 4.2  多变量梯度下降 4.3  梯度下降法实践 1-特征缩放 4.4  梯度下降法实践 2-学习率 4.5  特征和多项式回归 4.6  正规方程 4.7  正规方程及不可逆性 ...

  5. python实现多变量线性回归(Linear Regression with Multiple Variables)

    本文介绍如何使用python实现多变量线性回归,文章参考NG的视频和黄海广博士的笔记 现在对房价模型增加更多的特征,例如房间数楼层等,构成一个含有多个变量的模型,模型中的特征为( x1,x2,..., ...

  6. 4、、多变量线性回归(Linear Regression with Multiple Variables)

    4.1 多维特征 目前为止,我们探讨了单变量/特征的回归模型,现在我们对房价模型增加更多的特征,例如房间数楼层等,构成一个含有多个变量的模型,模型中的特征为(x1,x2,...xn) 增添更多特征后, ...

  7. Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable

    原文:http://blog.csdn.net/abcjennifer/article/details/7700772 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  8. 【原】Coursera—Andrew Ng机器学习—Week 2 习题—Linear Regression with Multiple Variables 多变量线性回归

    Gradient Descent for Multiple Variables [1]多变量线性模型  代价函数 Answer:AB [2]Feature Scaling 特征缩放 Answer:D ...

  9. 斯坦福机器学习视频笔记 Week2 多元线性回归 Linear Regression with Multiple Variables

    相比于week1中讨论的单变量的线性回归,多元线性回归更具有一般性,应用范围也更大,更贴近实际. Multiple Features 上面就是接上次的例子,将房价预测问题进行扩充,添加多个特征(fea ...

随机推荐

  1. Announcing the Operate Preview Release: Monitoring and Managing Cross-Microservice Workflows

    转自:https://zeebe.io/blog/2019/04/announcing-operate-visibility-and-problem-solving/   Written by Mik ...

  2. 学习Unity -- 理解依赖注入(IOC)三种方式依赖注入

    IOC:英文全称:Inversion of Control,中文名称:控制反转,它还有个名字叫依赖注入(Dependency Injection).作用:将各层的对象以松耦合的方式组织在一起,解耦,各 ...

  3. c#利用ApplicationContext类 同时启动双窗体的实现

    Application类(位于System.Windows.Forms命名空间)公开了Run方法,可以调用该方法来调度应用程序进入消息循环.Run方法有三个重载 1.第一个重载版本不带任何参数,比较少 ...

  4. mongodb集群配置主从模式

    测试环境 操作系统:CentOS 7.2 最小化安装 主服务器IP地址:192.168.197.21 master-node 从服务器IP地址:192.168.197.22 slave-node 关闭 ...

  5. TF(2): 核心概念

    TF的核心是围绕Graph展开的,简而言之,就是Tensor沿着Graph传递闭包完成Flow的过程.所以在介绍Graph之前需要讲述一下符号编程.计算流图.梯度计算.控制流的概念. 张量(Tenso ...

  6. 几种流行Webservice框架

    一. 几个比较流行的Webservice框架: Apache Axis1.Apache Axis2.Codehaus XFire.Apache CXF.Apache Wink.Jboss  RESTE ...

  7. [UE4]Window Title Bar Area

    一.Window Title Bar Area.windows窗口拖拽控件 二.window Buttons Enabled,在控件的右上角显示:最小化.最大化,关闭按钮; Toggle Fullsc ...

  8. Unity Shader Graph(一)初次尝试

    软件环境 Unity Version: 2018.1.2f1 边缘发光材质效果 创建工程 打开Unity并创建一个新工程 安装依赖项 Window -> Package Manager打开包管理 ...

  9. public class PageRender implements ResponseRender

    package cn.ubibi.jettyboot.demotest.controller.render; import cn.ubibi.jettyboot.framework.commons.S ...

  10. Java笔试面试题整理第六波(修正版)

    转载至:http://blog.csdn.net/shakespeare001/article/details/51330745 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...