Exercise 1:Linear Regression---实现一个线性回归

重要公式

1.h(θ)函数

2.J(θ)函数

思考一下,在matlab里面怎么表达?如下:

原理如下:(如果你懂了这道作业题,上面的也就懂了)

下面通过图形方式感受一下代价函数 :

3.θ迭代过程(梯度下降)

First way:批梯度下降:(编程作业使用这个公式,sum转换同理J(θ))

Second way:随机梯度下降:

好比我们下山,每次在一点环顾四周,往最陡峭的路向下走,用图形的方式更形象的表示 :

4.θ的直接求解法(让代价函数导数为0,求θ值)

编程作业答案(红色为添加代码)

1.warmUpExercise.m

function A = warmUpExercise()
%WARMUPEXERCISE Example function in octave
% A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix % ============= YOUR CODE HERE ==============
% Instructions: Return the 5x5 identity matrix
% In octave, we return values by defining which variables
% represent the return values (at the top of the file)
% and then set them accordingly. A = eye(5,5); % =========================================== end

2.plotData.m

function plotData(x, y)
%PLOTDATA Plots the data points x and y into a new figure
% PLOTDATA(x,y) plots the data points and gives the figure axes labels of
% population and profit. figure; % open a new figure window % ====================== YOUR CODE HERE ======================
% Instructions: Plot the training data into a figure using the
% "figure" and "plot" commands. Set the axes labels using
% the "xlabel" and "ylabel" commands. Assume the
% population and revenue data have been passed in
% as the x and y arguments of this function.
%
% Hint: You can use the 'rx' option with plot to have the markers
% appear as red crosses. Furthermore, you can make the
% markers larger by using plot(..., 'rx', 'MarkerSize', 10); plot(x,y, 'rx', 'MarkerSize', 10);
xlabel('Population of City in 10,000s');
ylabel('Profit in $10,000s'); % ============================================================ end

3.gradientDescent.m

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(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.
%
% ============================================================
  theta = theta - (alpha/m)*X'*(X*theta-y); % theta 就是用上面的向量表示法的 matlab 语言实现
    % Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta); end end

4.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 = 0; % ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost. J=1/(2*m)*(X*theta-y)'*(X*theta-y); % ========================================================================= end

5.运行结果

                      For population = 35,000, we predict a profit of 4519.767868
                      For population = 70,000, we predict a profit of 45342.450129

Stanford coursera Andrew Ng 机器学习课程第二周总结(附Exercise 1)的更多相关文章

  1. Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 2)及总结

    Exercise 1:Linear Regression---实现一个线性回归 关于如何实现一个线性回归,请参考:http://www.cnblogs.com/hapjin/p/6079012.htm ...

  2. Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 1)

    Exercise 1:Linear Regression---实现一个线性回归 在本次练习中,需要实现一个单变量的线性回归.假设有一组历史数据<城市人口,开店利润>,现需要预测在哪个城市中 ...

  3. Stanford coursera Andrew Ng 机器学习课程第四周总结(附Exercise 3)

    Introduction Neural NetWork的由来 时,我们可以对它进行处理,分类.但是当特征数增长为时,分类器的效率就会很低了. Neural NetWork模型 该图是最简单的神经网络, ...

  4. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 15—Anomaly Detection异常检测

    Lecture 15 Anomaly Detection 异常检测 15.1 异常检测问题的动机 Problem Motivation 异常检测(Anomaly detection)问题是机器学习算法 ...

  5. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 14—Dimensionality Reduction 降维

    Lecture 14 Dimensionality Reduction 降维 14.1 降维的动机一:数据压缩 Data Compression 现在讨论第二种无监督学习问题:降维. 降维的一个作用是 ...

  6. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 12—Support Vector Machines 支持向量机

    Lecture 12 支持向量机 Support Vector Machines 12.1 优化目标 Optimization Objective 支持向量机(Support Vector Machi ...

  7. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 1_Introduction and Basic Concepts 介绍和基本概念

    目录 1.1 欢迎1.2 机器学习是什么 1.2.1 机器学习定义 1.2.2 机器学习算法 - Supervised learning 监督学习 - Unsupervised learning  无 ...

  8. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 17—Large Scale Machine Learning 大规模机器学习

    Lecture17 Large Scale Machine Learning大规模机器学习 17.1 大型数据集的学习 Learning With Large Datasets 如果有一个低方差的模型 ...

  9. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 16—Recommender Systems 推荐系统

    Lecture 16 Recommender Systems 推荐系统 16.1 问题形式化 Problem Formulation 在机器学习领域,对于一些问题存在一些算法, 能试图自动地替你学习到 ...

随机推荐

  1. T1683 车厢重组 codevs

    http://codevs.cn/problem/1683/  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 白银 Silver 题目描述 Description 在一个旧式的火车 ...

  2. Spring/Maven/MyBatis配置文件结合properties文件使用

    使用properties文件也叫注入,比如把一些常用的配置项写入到这个文件,然后在Spring的XML配置文件中使用EL表达式去获取. 这种方式不只Spring可以使用,同样MyBatis也可以使用, ...

  3. MySQL: 让MySQL支持颜文字emoji

    数据库需要支持utf8mb4,如果已经创建了,那么可以用如下命令转换: alter database xxxx character set utf8mb4 collate utf8mb4_unicod ...

  4. keil编译后生成的M51文件解析

    通过*.m51文件我们能够清楚的看到单片机存储器的使用情况,以及能够看到每一个变量包含局部变量的位置. 之前碰到一个问题,相同的程序,在small模式下编译后执行没问题,但在large模式下能够编译. ...

  5. JAVA 网络长短连接

       作为java的刚開始学习的人,看了网上的资料后,关于java的长短连接,感觉理解的不是非常深刻.结合自己的学习和网上的资料整理例如以下.不对之处请大家批评指正.                  ...

  6. MongoDB 开机自启动

    MongoDB安装了以后,应当设置开机自启动. 假设启动命令如下: sudo /db/mongodb/265/bin/mongod --config /db/conf/mongodb/mongodb. ...

  7. 【Ubuntu】某灯图标过大

    发现某灯在桌面上图标过大,点击resize拖动没反应,遂查找原因 发现是它图标只有128x128的版本,所以显得比别的图标大 打开lantern.desktop(不知道.desktop请看https: ...

  8. 使用Vitamio插件显示花屏

    Vitamio是一款 Android 与 iOS 平台上的全能多媒体开发框架,全面支持硬件解码与 GPU 渲染. 使用vitamio进行播放器的开发非常便捷,使用vitamio的解码,自己编写播放器界 ...

  9. css list menu

    选择让page和folder都显示出来

  10. 动态DNS——本质上是IP变化,将任意变换的IP地址绑定给一个固定的二级域名。不管这个线路的IP地址怎样变化,因特网用户还是可以使用这个固定的域名 这样看的话,p2p可以用哇

    动态域名是因应网络远程访问的需要而产生的一项应用技术.因为没有固定IP,只能运用二级域名来应对经常变化的IP,动态域名的由来因此而产生. 它当前主要应用在:路由器.网络摄像机.带网络监控的硬盘录像机. ...