Coursera-吴恩达-机器学习-(编程练习1)Linear Regression(对应第1-2周课程)

本文档为Coursera上Andrew Ng教授的机器学习课程编程练习笔记,详细介绍了如何完成线性回归任务,包括实现代价函数计算、梯度下降算法等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此系列为 Coursera 网站Andrew Ng机器学习课程个人学习笔记(仅供参考)
课程网址:https://www.coursera.org/learn/machine-learning

exercise 1 —— Linear Regression

目录

1-1 作业介绍

下载作业包

在此下载第一次编程任务

此ZIP文件包含PDF的说明。
注意: PDF是最好的说明说,里面详细的说明了每一步操作,和下一步操作,一步步走即可。

在此下载Coursera-吴恩达-机器学习-全部编程练习答案

介绍一下里面的文件:
(1)主文件ex1.m

它是整个任务的流程文件,他自动调用其他文件,运行他可以一步步走完每个小任务。但是我们并不需要修改这个文件,只需要把它调用的代码文件 挨个修改即可。

(2) computeCost.m

计算代价函数的文件。我们要在里面填空。填空完成,再次运行ex1.m 即可。

(3)gradientDescent.m

计算梯度下降的文件,我们要在里面填空。填空完成,再次运行ex1.m 即可使用我们自己的梯度下降算法。

这里写图片描述
这里写图片描述

1-2作业分析

part1

首先我们来看 第一个小作业,ex1.m中的作业要求如下:

%% ==================== Part 1: Basic Function ====================
% Complete warmUpExercise.m
fprintf('Running warmUpExercise ... \n');
fprintf('5x5 Identity Matrix: \n');
warmUpExercise()

fprintf('Program paused. Press enter to continue.\n');
pause;

意思就是,通过修改warmUpExercise.m这个文件,让主程序调用它时能产生一个5*5的矩阵。那么我们就去修改这个文件,在文件包里找到这个文件,然后在原来代码的基础上添加一句话即可:

A = eye(5);   % 生成一个5*5的矩阵

总样子为:

function A = warmUpExercise()
%WARMUPEXERCISE Example function in octave
%   A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix

A = [];
% ============= 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);

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

end

结果为:

这里写图片描述

part2—part4

依次找到对应的文件,进行修改,然后重新运行ex1.m即可
步骤及结果依次是:
(1)画出数据集
这里写图片描述
(2)梯度下降,找出使得代价函数最小化的最佳参数
这里写图片描述
这里写图片描述
(3)求出回归方程,画出拟合曲线
这里写图片描述

Linear regression with multiple variables

自己尝试去做一下,这里就不列举了。

Programming Exercise 1: Linear Regression Machine Learning Introduction In this exercise, you will implement linear regression and get to see it work on data. Before starting on this programming exercise, we strongly recom- mend watching the video lectures and completing the review questions for the associated topics. To get started with the exercise, you will need to download the starter code and unzip its contents to the directory where you wish to complete the exercise. If needed, use the cd command in Octave/MATLAB to change to this directory before starting this exercise. You can also find instructions for installing Octave/MATLAB in the “En- vironment Setup Instructions” of the course website. Files included in this exercise ex1.m - Octave/MATLAB script that steps you through the exercise ex1 multi.m - Octave/MATLAB script for the later parts of the exercise ex1data1.txt - Dataset for linear regression with one variable ex1data2.txt - Dataset for linear regression with multiple variables submit.m - Submission script that sends your solutions to our servers [?] 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 ? indicates files you will need to complete † indicates optional exercises
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值