代价函数cost function

  • 公式:



    其中,变量θ(Rn+1或者R(n+1)*1

  • 向量化:

Octave实现:

  1. function J = computeCost(X, y, theta)
  2. %COMPUTECOST Compute cost for linear regression
  3. % J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
  4. % parameter for linear regression to fit the data points in X and y
  5. % Initialize some useful values
  6. m = length(y); % number of training examples
  7. % You need to return the following variables correctly
  8. J = 0;
  9. % ====================== YOUR CODE HERE ======================
  10. % Instructions: Compute the cost of a particular choice of theta
  11. % You should set J to the cost.
  12. prediction=X*theta;
  13. sqerror=(prediction-y).^2;
  14. J=1/(2*m)*sum(sqerror)
  15. % =========================================================================
  16. end

多变量梯度下降(gradient descent for multiple variable)

  • 公式:



    也即,

  • 矩阵化:

    梯度下降可以表示为,



    其中,为,



    其中微分可以求得,



    将其向量化后,



    则最终的梯度下降的矩阵化版本,

Octave版本:

  1. function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
  2. %GRADIENTDESCENT Performs gradient descent to learn theta
  3. % theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
  4. % taking num_iters gradient steps with learning rate alpha
  5. % Initialize some useful values
  6. m = length(y); % number of training examples
  7. J_history = zeros(num_iters, 1);
  8. for iter = 1:num_iters
  9. % ====================== YOUR CODE HERE ======================
  10. % Instructions: Perform a single gradient step on the parameter vector
  11. % theta.
  12. %
  13. % Hint: While debugging, it can be useful to print out the values
  14. % of the cost function (computeCost) and gradient here.
  15. %
  16. predictions=X*theta;
  17. updates=X'*(predictions-y);
  18. theta=theta-alpha*(1/m)*updates;
  19. % ============================================================
  20. % Save the cost J in every iteration
  21. J_history(iter) = computeCost(X, y, theta);
  22. end
  23. end

ML:多变量代价函数和梯度下降(Linear Regression with Multiple Variables)的更多相关文章

  1. Linear regression with multiple variables(多特征的线型回归)算法实例_梯度下降解法(Gradient DesentMulti)以及正规方程解法(Normal Equation)

    ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, , ...

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

    机器学习(三)--------多变量线性回归(Linear Regression with Multiple Variables) 同样是预测房价问题  如果有多个特征值 那么这种情况下  假设h表示 ...

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

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

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

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

  5. Machine Learning – 第2周(Linear Regression with Multiple Variables、Octave/Matlab Tutorial)

    Machine Learning – Coursera Octave for Microsoft Windows GNU Octave官网 GNU Octave帮助文档 (有900页的pdf版本) O ...

  6. 斯坦福机器学习视频笔记 Week1 线性回归和梯度下降 Linear Regression and Gradient Descent

    最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...

  7. 机器学习之多变量线性回归(Linear Regression with multiple variables)

    1. Multiple features(多维特征) 在机器学习之单变量线性回归(Linear Regression with One Variable)我们提到过的线性回归中,我们只有一个单一特征量 ...

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

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

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

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

随机推荐

  1. 使用JSONlib简单的转换json操作

    在使用jsonlib操作之前 需要引入json及5个依赖包,依赖包版本不能最新,lang与collections有不兼容现象. 官方地址:http://json-lib.sourceforge.net ...

  2. [Clojure] A Room-Escape game, playing with telnet and pure-text commands - Part 3

    Code Path: https://github.com/bluesilence/Lisp/blob/master/clojure/projects/room-escape/src/room_esc ...

  3. 如何在CSDN博客自定义栏目中添加“给我写信”

    在"自定义栏目"中添加"连接"(将自己的微博,QQ空间和CSDN博客关联起来)很多人都做过.但是添加"给我写信"这个功能,用的好像不太多.此 ...

  4. 用Github账号领Ripple币

    最近随着比特币在互联网上的流行,其他各种电子货币也都增加了曝光率. 昨晚在 v2ex 上看到有人发帖,用 20RMB 换取 2013 年 5 月 1 日前使用过的 github 账号得到的一个验证码. ...

  5. JAVA SE回顾及思考(2)——数组的复制与动态扩展

    我们知道在Java中数组是非基本类型既数组是对象(Object)的子类,所以用下面的这种方式是不能复制该对象的 public static void main(String[] args) { int ...

  6. Excel创button宏调用

    今天,匆匆写了一个宏,但发现已被用来创建button开发工具菜单不见了. 在十分钟找Excel转了个遍,终究Excel通常使用在中的选项,首先Mark下一个,离开同样找不到鞋. 几个截面图.促进突然, ...

  7. OpenCV图像的基础叠加

    程序及分析 /* * FileName : blend.cpp * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : Mon 28 Ju ...

  8. 5 wcf双工

    1 创建两个项目 Wcf_WsDual_Service   Wcf_WsDual_Client 2 Wcf_WsDual_Service   项目中创建 LoginService.cs 的wcf文件 ...

  9. “warning C4996: 'fopen': This function or variable may be unsafe”和“LINK : fatal error LNK1104”的解决办法

    程序有时编译出现警告C4996,报错:  warning C4996: 'fopen': This function or variable may be unsafe. Consider using ...

  10. C#中HttpWebRequest与HttpWebResponse的使用方法

    关键字:C# HttpWebRequest HttpWebResponse HTTP GET POST 请求 这个类是专门为HTTP的GET和POST请求写的,解决了编码,证书,自动带Cookie等问 ...