机器学习之练习材料一(线性回归)

Linear Regression(线性回归)

1 Simple Octave/MATLAB function

这里的第一个练习是写一个简单生成5*5的单元矩阵的函数,找到warmUpExercise.m文件,代码实现如下:

function A = warmUpExercise() 

%WARMUPEXERCISE Example function inoctave

%A = WARMUPEXERCISE() is an example function that returns the5x5 identity matrix

A = [];

% ========== YOUR CODE HERE =============

% Instructions: Return the5x5 identity matrix

%                     In octave,  we return values by defining which variables

%                     represent there turn values (at the top of the file)

%                     and then set them accordingly.

A = eye(5);

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

end

输出如图T1.1(在这里使用Octave-gui编译工具):

机器学习之练习材料一(线性回归)_第1张图片
T1.1


2 Linear regression with one variable(单变量线性回归)



第二个练习,假设你是一个餐厅专营公司的CEO,并正在考虑开设新的路线,你已经有了各个城市的利润与人数之间的关系的数据集,然后去实现一个单变量的线性回归算法来预测食物卡车的利润。

2.1 Plotting the Data(绘制数据集)

在ex1.m 里面已经有绘制数据集的代码,直接执行即可;代码如图T2.1.1,绘图效果如图T2.1.2。

在这之前还需要完善plotData.m的代码,设置横纵坐标的标题意义:

plot(x, y,'rx','MarkerSize', 10);                  % Plot the data

ylabel('Profit in $10,000s');                       % Set the yaxis label

xlabel('Population of City in 10,000s');      % Set the xaxis label


机器学习之练习材料一(线性回归)_第2张图片
T2.1.1

X是取exdata1.txt 文件的第一列数据,Y是取exdate1.txt文件的第二列数据。

机器学习之练习材料一(线性回归)_第3张图片
T2.1.2

从图中可以看出,图中每一个点相当于一行data数据集的数据。相当于每个城市的人口对应的利润是多少美元。

2.2 Gradient Descent(梯度下降)

这里主要使用梯度下降算法来迭代更新成本函数。

2.2.1 Update Equations(更新方程式)

成本函数和期望函数如图T2.21.A:

机器学习之练习材料一(线性回归)_第4张图片
T2.2.1.A

梯度函数如图T2.2.1.B:


机器学习之练习材料一(线性回归)_第5张图片
T2.2.1.B

2.2.2 Implementation(执行)

这里设置函数需要的各个参数值(X矩阵,斯塔值,迭代次数,阿米伽值)。

X = [ones(m, 1), data(:,1)]; % Add a column of ones to x

theta = zeros(2, 1);% initialize fitting parameters

iterations = 1500;

alpha = 0.01;

2.2.3 Computing the cost J(theta)

这里需要完善computerCost.m代码.

function J = computeCost(X, y, theta)

%COMPUTECOST Compute costforlinear regression

%     J = COMPUTECOST(X, y, theta) computes the cost of using theta as the

%     parameterforlinear regression to fit the data pointsinX and y

%     Initialize some useful values

m = length(y); % number of training examples

%     You need toreturnthe 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 = sum((X*theta - y).^2) / (2*m);

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

end

完成后即可执行ex1.m中代码;效果如图T2.2.3:

机器学习之练习材料一(线性回归)_第6张图片
T2.2.3

2.2.4 Gradient descent(梯度下降)

这里需要完善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,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 printoutthe values

               % of the cost function (computeCost) and gradient here.

               %

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

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

              % Save the cost Jinevery iteration

              J_history(iter) = computeCost(X, y, theta);

end

end

完成后按照ex1.m的代码继续执行,效果如图T2.24:

机器学习之练习材料一(线性回归)_第7张图片
T2.24

2.3 Debugging(调试)

这里只需要执行ex1.m的代码即可调试结果;代码效果如图:T2.3.1,绘图效果如图:T2.3.2

T2.3.1


机器学习之练习材料一(线性回归)_第8张图片
T2.3.2

继续执行ex1.m中的代码,效果如图T2.3.3:


机器学习之练习材料一(线性回归)_第9张图片
2.3.3

这里可以得到预测结果:

35000人口时利润为:$4519.76。

70000人口时利润为:$45342.45。

2.4 Visualizing J(theat)  (成本函数视图化)

执行ex1.m中代码即可,代码效果如图T2.4.1;3D效果视图为T2.4.2;2D效果视图为T2.4.3。

机器学习之练习材料一(线性回归)_第10张图片
T2.4.1


机器学习之练习材料一(线性回归)_第11张图片
T2.4.2


机器学习之练习材料一(线性回归)_第12张图片
T2.4.3

你可能感兴趣的:(机器学习之练习材料一(线性回归))