必做:

[*] 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. superobject使用方法

    superobject使用方法 ISuperObject.AsObject 可获取一个 TSuperTableString 对象. TSuperTableString 的常用属性: count.Get ...

  2. 【java】java自带的java.util.logging.Logger日志功能

    偶然翻阅到一篇文章,注意到Java自带的Logger日志功能,特地来细细的看一看,记录一下. 1.Java自带的日志功能,默认的配置 ①Logger的默认配置,位置在JRE安装目录下lib中的logg ...

  3. 关于select 控件

    通过http://www.w3school.com.cn/tiy/t.asp?f=html_select 的测试,测得,select 控件值最多106个. Q:easyui的datagrid中能做到 ...

  4. 使用ajax,结合jquery,php实现图片上传预览功能

    大致逻辑:点击页面的file,上传图片到指定的php处理图片的文件,处理完成以后,将图片的连接地址返回,JS控制返回的数据,然后将图片动态的展示出来html代码<label> <im ...

  5. 如何把自己的代码发布到npmjs(npm publish)

    来源: https://www.cnblogs.com/calamus/p/8384318.html

  6. 批处理创建数据库(Sql Server)

    ylbtech-Miscellaneos:批处理创建数据库(Sql Server) 1.A,资源(Resource) - 创建数据返回顶部 1.A.1,InstallDatabases.cmd - 编 ...

  7. [转载]linux 清除系统cached

    FROM: http://cqfish.blog.51cto.com/622299/197230 linux 清除系统cached top查看系统内存使用情况   Mem:    16432180k ...

  8. vscode - emmet失效?

    把emmet设置覆盖为用户.

  9. svn解决与优化帮助

    1.问题的出现 解决方案: 最后一行不能子目录. 启动的时候也是要根目录的.svnserve -d -r /home/svn/repos [不能是svnserve -d -r /home/svn/re ...

  10. ESLint检测JavaScript代码

    1.安装 有2中安装方式:全局安装和局部安装. 局部安装方式为: (1)cnpm install -g eslint (2)打开项目目录.配置eslint文件 eslint --init (3)执行e ...