[TOC]
1. Introduction
任务所要用到的文件
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
2. Assignment
2.1 Simple Octave/MATLAB function
这一步很简单,在写好的函数内添加内容
打开warmUpExercise.m文件
1 | function A = warmUpExercise() |
2.2 Linear regression with one variable
这一节有三个任务:
- 可视化数据
- 计算梯度下降
- 结果可视化
2.2.1 Plotting the Data
导入'ex1data1.txt
文件数据,然后显示
首先写一个显示数据的函数plotData
保存到文件plotData.m
1 | function plotData(x, y) |
plot使用
导入数据,调用函数plotData
:
ex1data1.txt
:
1 | 6.1101,17.592 |
1 | data = load('ex1data1.txt'); |
结果展示:
2.2.2 Gradient Descent
首先计算损失函数,公式如下:
代码文件computeCost.m
1 | function J = computeCost(X, y, theta) |
然后计算梯度下降,公式如下:
代码文件gradientDescent.m
1 | function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters) |
最后绘制回归曲线
1 | theta = gradientDescent(X, y, theta, alpha, iterations); |
legend 为每个绘制的数据序列创建一个带有描述性标签的图例。
2.2.3 Visualizing J(θ)
损失值可视化,这部分不用自己写代码。首先通过linspace 取一些范围theta值。然后调用computeCost获取每一组theta值的cost。
1 | % Grid over which we will calculate J |
获取到cost之后调用surf 函数画出来一个三维曲面图。代码如下:
1 | % Because of the way meshgrids work in the surf command, we need to |
1 | % Contour plot |
2.3 Linear regression with multiple variables
2.3.1 特征归一化(Feature Normalization)
代码文件featureNormalize.m
1 | function [X_norm, mu, sigma] = featureNormalize(X) |
2.3.2 Gradient Descent
对于矩阵向量之间的计算多变量和单变量的公式都一样。所以computeCostMulti.m
and gradientDescentMulti.m
两个文件代码请看前面。
绘制成本函数曲线:
1 | % Choose some alpha value |
2.3.3 Normal Equations
寻找最优参数theta,还可以通过正规方程实现,不用设置学习率α和特征缩放,也不用计算梯度下降,迭代至收敛。公式:
代码文件:normalEqn.m
1 | function [theta] = normalEqn(X, y) |
至此,作业1完成,Congratulation!