❄❄❄❄❄❄❄❄【回到目录】❄❄❄❄❄❄❄❄

本次编程作业中,需要完成的代码有如下几部分:

[⋆] 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
[†] computeCostMulti.m - Cost function for multiple variables
[†] gradientDescentMulti.m - Gradient descent for multiple variables
[†] featureNormalize.m - Function to normalize features
[†] normalEqn.m - Function to compute the normal equations

1  warmUpExercise.m   --- Octave/MATLAB的简单函数

在文件warmUpExercise.m中,您将看到Octave / MATLAB函数的概要。通过填写以下代码修改它以返回5 x 5单位矩阵:

function A = warmUpExercise()
%WARMUPEXERCISE Example function in octave
%   A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix
% ============= YOUR CODE HERE ==============
A = eye(5);
% ========================================== end 

完成之后,运行ex1.m,就可以看到类似于以下内容的输出:

2  plotData.m  ---  将数据绘制成图像

绘制图形可以帮助我们可视化数据。对于此数据集,我们可以使用散点图来绘制数据,因为它只有收益和人口两个数据(在现实生活中很多问题都是多维数据表示,并不能绘制成二维图)。

加载数据:

data = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples

接下来,该程序调用plotData子函数来绘制数据的散点图。 你的工作是完成plotData.m函数来绘制图像;,修改代码:

function plotData(x, y)

% ====================== YOUR CODE HERE ======================
% Hint: 您可以在绘图中使用'rx'选项,使标记显示为红色十字。
%       此外,您可以使用plot(...,'rx','MarkerSize',10)
%       使标记更大

figure; % 打开一个新的数字窗口
plot(x, y, 'rx','MarkerSize', 10);    %r代表red; x 代表十字标记 ;10是标记的大小
ylabel('Profit in $10,000s');          %加y轴的标签
xlabel('Population of City in 10,000s');
% ============================================================
end

plot( )函数的使用可以参考该文档

运行ex1.m之后,你可以看到Figure 1。

3 computeCost.m --- 计算代价函数

线性回归的目的就是最小化代价函数:

其中假设函数hθ(x)是一个线性模型:hθ(x) = θT x = θ0 + θ1x1

当您执行梯度下降从而最小化成本函数J(θ)时,通过计算成本cost有助于监视是否收敛。 在本节中,您将实现一个计算J(θ)的函数,以便检查梯度下降实现的收敛性。

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 

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
%               You should set J to the cost.

J = (1/(2*m))*sum(((X*theta) - y).^2)

% =========================================================================

end

完成该子程序后,ex1.m中将使用零初始化的 θ 运行computeCost,您将该结果: 

4 gradientDescent.m --- 运行梯度下降

接下来你的任务是完成gradientDescent.m,从而实现梯度下降。在编程时,请确保你了解要优化的内容和正在更新的内容。 请记住,代价函数J(θ)参数 θ 的函数,而不是X和y。 也就是说,我们通过改变参数θ的值而不是通过改变X或y来最小化J(θ)。验证梯度下降是否正常工作的一种好方法是查看J(θ)的值,并检查它是否随着每一次迭代减小。 如果正确实现了梯度下降和computeCost,则J(θ)的值不应该增加,并且应该在算法结束时收敛到稳定值。

我们通过调整参数 θ 的值从而最小化代价函数 J(θ)。通过batch梯度下降可以达到目的。在梯度下降中,每次迭代都执行下面的这个更新:

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, 1);

for iter = 1: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*(1/m)*X'*(X*theta)-y);
    % ============================================================

    % Save the cost J in every iteration
    J_history(iter) = computeCost(X, y, theta);

end

end

当你完成之后, 完成后,ex1.m将使用最终参数来绘制线性逼近。 结果应如下图所示:

5 featureNormalize.m --- 特征缩放

数据中,房屋大小的数量级约为卧室数量的1000倍。 当特征相差几个数量级时,首先执行特征缩放可以使梯度下降更快地收敛。特征缩放的方式有以下几种:【特征缩放】(4.3节)

你的任务是完成featureNormalize.m

function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X
%   FEATURENORMALIZE(X) returns a normalized version of X where
%   the mean value of each feature is 0 and the standard deviation
%   is 1. This is often a good preprocessing step to do when
%   working with learning algorithms.

% You need to set these values correctly
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));

% ====================== YOUR CODE HERE ======================
% Instructions: First, for each feature dimension, compute the mean
%               of the feature and subtract it from the dataset,
%               storing the mean value in mu. Next, compute the
%               standard deviation of each feature and divide
%               each feature by it's standard deviation, storing
%               the standard deviation in sigma.
%
%               Note that X is a matrix where each column is a
%               feature and each row is an example. You need
%               to perform the normalization separately for
%               each feature.
%
% Hint: You might find the 'mean' and 'std' functions useful.
%
mu = mean(X_norm);
sigma = std(X_norm );
X_norm(:,1) = ((X_norm(:,1)-mu(1)))./sigma(1);
X_norm(:,2) = ((X_norm(:,2)-mu(2)))./sigma(2);

% ============================================================

end

6 computeCostMulti.m --- 计算多变量的代价函数

之前已经完成了单变量线性回归中的计算代价函数和梯度下降,多变量的和单变量基本一致,唯一不同的是数据X矩阵中多了一个特征。

注意:在多变量的情况下,代价函数也可以使用以下的向量化形式编写:(向量化形式会使得计算更加高效)

function J = computeCostMulti(X, y, theta)
%COMPUTECOSTMULTI Compute cost for linear regression with multiple variables
%   J = COMPUTECOSTMULTI(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 = (X*theta-y)'*(X*theta-y);   %或者 J = (1/(2*m))*sum(((X*theta) - y).^2);

% =========================================================================

end

7 gradientDescentMulti.m --- 多变量的梯度下降

function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
%GRADIENTDESCENTMULTI Performs gradient descent to learn theta
%   theta = GRADIENTDESCENTMULTI(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, 1);

for iter = 1: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 (computeCostMulti) and gradient here.
    %

    theta = theta - (alpha/m)*(X')*(X*theta - y);

    % ============================================================

    % Save the cost J in every iteration
    J_history(iter) = computeCostMulti(X, y, theta);

end

end

8 normalEqn.m --- 正规方程

区别于迭代的方式,还可以用正规方程来求解:

使用此公式不需要任何特征缩放,可以在一次计算中得到一个精确的解决方案。

function [theta] = normalEqn(X, y)
%NORMALEQN Computes the closed-form solution to linear regression
%   NORMALEQN(X,y) computes the closed-form solution to linear
%   regression using the normal equations.

theta = zeros(size(X, 2), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the code to compute the closed form solution
%               to linear regression and put the result in theta.
%
% ---------------------- Sample Solution ----------------------

theta = pinv(X'*X)*(X'*y)

% -------------------------------------------------------------

% ============================================================

end 

如果这篇文章帮助到了你,或者你有任何问题,欢迎扫码关注微信公众号:一刻AI  在后台留言即可,让我们一起学习一起进步!

吴恩达《机器学习》编程作业——machine-learning-ex1:线性回归的更多相关文章

  1. coursera吴恩达 机器学习编程作业原文件 及我的作业

    保存在github上供广大网友下载:点击 8个zip,原文件,没有任何改动. 另外,不定期上传我自己关于这门课的学习过程笔记和心得,有兴趣的盆友可以点击这里查看.

  2. 【吴恩达课后编程作业】第二周作业 - Logistic回归-识别猫的图片

    1.问题描述 有209张图片作为训练集,50张图片作为测试集,图片中有的是猫的图片,有的不是.每张图片的像素大小为64*64 吴恩达并没有把原始的图片提供给我们 而是把这两个图片集转换成两个.h5文件 ...

  3. ML:吴恩达 机器学习 课程笔记(Week1~2)

    吴恩达(Andrew Ng)机器学习课程:课程主页 由于博客编辑器有些不顺手,所有的课程笔记将全部以手写照片形式上传.有机会将在之后上传课程中各个ML算法实现的Octave版本. Linear Reg ...

  4. 用纯Python实现循环神经网络RNN向前传播过程(吴恩达DeepLearning.ai作业)

    Google TensorFlow程序员点赞的文章!   前言 目录: - 向量表示以及它的维度 - rnn cell - rnn 向前传播 重点关注: - 如何把数据向量化的,它们的维度是怎么来的 ...

  5. Machine Learning——吴恩达机器学习笔记(酷

    [1] ML Introduction a. supervised learning & unsupervised learning 监督学习:从给定的训练数据集中学习出一个函数(模型参数), ...

  6. Machine Learning|Andrew Ng|Coursera 吴恩达机器学习笔记

    Week1: Machine Learning: A computer program is said to learn from experience E with respect to some ...

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

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

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

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

  9. Machine Learning|Andrew Ng|Coursera 吴恩达机器学习笔记(完结)

    Week 1: Machine Learning: A computer program is said to learn from experience E with respect to some ...

  10. ML:吴恩达 机器学习 课程笔记(Week5~6)

    Neural Networks: Learning Advice for Applying Machine Learning Machine Learning System Design

随机推荐

  1. 类System

    System类简介: 在 System 类中提供了大量的静态方法,有标准输入.标准输出和错误输出流:对外部定义的属性和环境变量的访问:加载文件和库的方法:还有快速复制数组的一部分的实用方法. 常用方法 ...

  2. P4145 上帝造题的七分钟2 / 花神游历各国(线段树区间开平方)

    有点意思,不需要什么懒标记之类的东西,因为一个数无论怎样开平方,最后取整的结果必然会是1,所以我们不妨用最大值来维护,若区间最大值不为1,就暴力修改,否则不用管. #include<bits/s ...

  3. 深度学习结合SLAM研究总结

    博客转载自:https://blog.csdn.net/u010821666/article/details/78793225 原文标题:深度学习结合SLAM的研究思路/成果整理之 1. 深度学习跟S ...

  4. PLC 数据类型

    类型 长度(位) 取值范围 描述 BOOL 1 0/1 布尔型 BYTE 8 0x00~0xFF 十六进制数 WORD 16 0~65535 无符号整数 DWORD 32 0~4294967295 无 ...

  5. [CF 666E] Forensic Examination

    Description 传送门 Solution 对 \(T[1..m]\) 建立广义后缀自动机,离线,找出代表 \(S[pl,pr]\) 的每个节点,线段树合并. Code #include < ...

  6. 最小生成树——Prim算法和Kruskal算法

    洛谷P3366 最小生成树板子题 这篇博客介绍两个算法:Prim算法和Kruskal算法,两个算法各有优劣 一般来说当图比较稀疏的时候,Kruskal算法比较快 而当图很密集,Prim算法就大显身手了 ...

  7. Mac anaconda安装 “conda command not found” 解决方法

    官网下载包直接安装的时候可能会产生这种问题,这主要还是环境变量配置的问题 一般我们添加环境变量的方法是编辑.bash_profile或.bashrc,在文件里插入下面这段代码 export PATH= ...

  8. String方法,js中Array方法,ES5新增Array方法,以及jQuery中Array方法

    相关阅读:https://blog.csdn.net/u013185654/article/details/78498393 相关阅读:https://www.cnblogs.com/huangyin ...

  9. 图像风格迁移(Pytorch)

    图像风格迁移 最后要生成的图片是怎样的是难以想象的,所以朴素的监督学习方法可能不会生效, Content Loss 根据输入图片和输出图片的像素差别可以比较损失 \(l_{content} = \fr ...

  10. UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 0: ordinal not in range(128)