必做:

[*] warmUpExercise.m - Simple example function in Octave/MATLAB
[*] plotData.m - Function to display the dataset
[*] computeCost.m - Function to compute the cost of linear regression
[*] gradientDescent.m - Function to run gradient descent

1.warmUpExercise.m

A = eye();

2.plotData.m

plot(x, y, 'rx', 'MarkerSize', ); % Plot the data
ylabel('Profit in $10,000s'); % Set the y-axis label
xlabel('Population of City in 10,000s'); % Set the x-axis label

3.computeCost.m

function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y % Initialize some useful values
m = length(y); % number of training examples % You need to return the following variables correctly
J = ; % ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost. H = X*theta-y;
J = (1/(2*m))*sum(H.*H); % ========================================================================= end

公式:   

注意matlab中  .* 的用法。

4.gradientDescent.m

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha % Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, ); for iter = :num_iters % ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.      H = X*theta-y;
    theta(1)=theta(1)-alpha*(1/m)*sum(H.*X(:,1));
    theta(2)=theta(2)-alpha*(1/m)*sum(H.*X(:,2)); % ============================================================ % Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta); end end

单变量梯度下降

对函数J(θ)求偏导

即 H.*X(:,1)

θi向着梯度最小的方向减少,alpha为步长。

theta(i)=theta(i)-alpha*(1/m)*sum(H.*X(:,i));

Coursera machine learning 第二周 编程作业 Linear Regression的更多相关文章

  1. Coursera machine learning 第二周 quiz 答案 Linear Regression with Multiple Variables

    https://www.coursera.org/learn/machine-learning/exam/7pytE/linear-regression-with-multiple-variables ...

  2. Coursera machine learning 第二周 quiz 答案 Octave/Matlab Tutorial

    https://www.coursera.org/learn/machine-learning/exam/dbM1J/octave-matlab-tutorial Octave Tutorial 5  ...

  3. Coursera-AndrewNg(吴恩达)机器学习笔记——第二周编程作业

    一.准备工作 从网站上将编程作业要求下载解压后,在Octave中使用cd命令将搜索目录移动到编程作业所在目录,然后使用ls命令检查是否移动正确.如: 提交作业:提交时候需要使用自己的登录邮箱和提交令牌 ...

  4. Coursera-AndrewNg(吴恩达)机器学习笔记——第二周编程作业(线性回归)

    一.准备工作 从网站上将编程作业要求下载解压后,在Octave中使用cd命令将搜索目录移动到编程作业所在目录,然后使用ls命令检查是否移动正确.如: 提交作业:提交时候需要使用自己的登录邮箱和提交令牌 ...

  5. 【Machine Learning】单参数线性回归 Linear Regression with one variable

        最近开始看斯坦福的公开课<Machine Learning>,对其中单参数的Linear Regression(未涉及Gradient Descent)做个总结吧. [设想]    ...

  6. Andrew Ng机器学习编程作业: Linear Regression

    编程作业有两个文件 1.machine-learning-live-scripts(此为脚本文件方便作业) 2.machine-learning-ex1(此为作业文件) 将这两个文件解压拖入matla ...

  7. [Machine Learning (Andrew NG courses)]II. Linear Regression with One Variable

  8. [Machine Learning (Andrew NG courses)]IV.Linear Regression with Multiple Variables

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenFoXzE5OTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  9. Coursera公开课-Machine_learing:编程作业

    第二周编程作业:Linear Regression 分为单一变量和多变量,假想函数为:hθ(x)=θ0+θ1x1+θ2x2+θ3x3+⋯+θnxn.明显已经包含单一变量的情况,所以完成多变量可以一并解 ...

随机推荐

  1. MySQL 5.7.17 Group Replication 初始

    http://blog.csdn.net/mchdba/article/details/53957248

  2. UVa116 (单向TSP,多决策问题)

    /*----UVa1347 单向TSP 用d(i,j)表示从格子(i,j)出发到最后一列的最小开销 则在(i,j)处有三种决策,d(i,j)转移到d(i-1,j+1),d(i,j+1),d(i+1,j ...

  3. django网站安全学习记录

    现在比较流行的网站攻击方式有sql注入,xss跨站脚本攻击,csrf跨站请求伪造,一句话木马等等 django非常强大,对这些攻击都做了防范 sql注入,通过在sql语句中插入非法的sql语句来实现爆 ...

  4. CentOS6.5安装ganglia3.6

    参考来源: 1.http://yhz.me/blog/Install-Ganglia-On-CentOS.html 2.http://blog.csdn.net/sdlyjzh/article/det ...

  5. 2017.6.30 IDEA插件--gsonfomat的安装与使用

    参考来自:http://www.cnblogs.com/1024zy/p/6370305.html 1.安装 2.使用 (1)新建一个空类 (2)在空类里按快捷键:alt+s,打开gsonformat ...

  6. XP中如何配置和共享打印机

    Win XP中如何配置和共享打印机                一.配置  打印机 在"控制面板"打开"打印机和传真",在左边的选项或单击右键选择" ...

  7. 尖峰冲击测试(spike Testing)

    与可靠性测试类似,尖峰冲击测试这种方法也是从其他行业借鉴而来.在电力工业,有一种冲击测试,用来验证设备在刚刚接通电源时能否经受住涌流的破坏.所谓涌流,通俗地说,就是电源接通瞬间,电流突然变大的现象.涌 ...

  8. JavaWeb Cookie详解

    代码地址如下:http://www.demodashi.com/demo/12713.html Cookie的由来 首先我们需要介绍一下,在Web开发过程中为什么会引入Cookie.我们知道Http协 ...

  9. svn hooks使用

    最近要将某个目录做samba共享出去,而想通过svn同步文档到svn,然后通过svn hooks 同步到共享目录,实现自动化 现在svn服务器和samba server再同一台机器上: 在svn路径下 ...

  10. rplidar 扫描角度设置

    参考网站::   https://blog.csdn.net/sunyoop/article/details/78302090 https://blog.csdn.net/dzhongjie/arti ...