Stanford Coursera Andrew Ng的机器学习编程作业——exercise 1 (Linear Regression)

exercise 1:完后单变量的线性回归练习

1.第一部分   让warmUpExercise.m中返回一个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

得到输出结果:

2.第二部分  采用线性回归的办法来预测餐车的利润,假设我是一家餐厅的CEO,我现在拥有很多城市的人口数量和利润的数据。负数表述我在该城市是亏损经营。我将利用这些数据来帮助我选择将哪个城市作为我下一步扩张的点。

在进行任务以前,将数据可视化可帮助我们更好的理解这些数据。

因为这道题中,只有两个参数需要绘制,我们采用散点图来表示。真实情况下,往往多于两个参数。

先定义画图的子函数:

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

其中,先来解释下plot函数的用法

 1, plot(x,y) 以x 元素为横坐标值,y 元素为纵坐标值绘制曲线。

 2, plot(x,y1,x,y2,…) 以公共的x 元素为横坐标值,以y1,y2,… 元素为纵坐标值绘制多条曲线。

上述程序中rx,表示画出来的散点以红十字表示

marker就是画上点的地方注上符号,而size就是大小了,这里为10

由xlabel和ylabel标记横纵坐标轴

然后在主函数中获得文本中的数据后,调用画图的函数

%% ======================= Part 2: Plotting =======================
fprintf('Plotting Data ...\n')
data = load('ex1data1.txt');                               //从文本获得数据
X = data(:, 1); y = data(:, 2);                            //X是第一列人口数,y是第二列利润
m = length(y); % number of training examples               //m是矩阵的维度,也是训练样本的个数

% Plot Data
% Note: You have to complete the code in plotData.m
plotData(X, y);

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

画得的图如下:

 

 3.第三部分  梯度下降算法

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

而线性回归中的假设函数为

在批量梯度下降中,不断更新调整θ的值,来使代价函数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 
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)*sum((X*theta-y).^2);


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

end

可以得到代价函数值为32.07

接下来更新θ的值,并进行1500次迭代获得J的值

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

并通过主函数调用显示:

%% =================== Part 3: Cost and Gradient descent ===================

X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1); % initialize fitting parameters

% Some gradient descent settings
iterations = 1500;
alpha = 0.01;

fprintf('\nTesting the cost function ...\n')
% compute and display initial cost
J = computeCost(X, y, theta);
fprintf('With theta = [0 ; 0]\nCost computed = %f\n', J);
fprintf('Expected cost value (approx) 32.07\n');

% further testing of the cost function
J = computeCost(X, y, [-1 ; 2]);
fprintf('\nWith theta = [-1 ; 2]\nCost   = %f\n', J);
fprintf('Expected cost value (approx) 54.24\n');

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

fprintf('\nRunning Gradient Descent ...\n')
% run gradient descent
theta = gradientDescent(X, y, theta, alpha, iterations);

% print theta to screen
fprintf('Theta found by gradient descent:\n');
fprintf('%f\n', theta);
fprintf('Expected theta values (approx)\n');
fprintf(' -3.6303\n  1.1664\n\n');

% Plot the linear fit
hold on; % keep previous plot visible
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')
hold off % don't overlay any more plots on this figure

% Predict values for population sizes of 35,000 and 70,000
predict1 = [1, 3.5] *theta;
fprintf('For population = 35,000, we predict a profit of %f\n',...
    predict1*10000);
predict2 = [1, 7] * theta;
fprintf('For population = 70,000, we predict a profit of %f\n',...
    predict2*10000);

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

先初始化,再假设了theta=[0;0]和theta=[-1;2]情况下的J值来测试

然后获得1500次迭代以后的theta和J值,并画出拟合曲线

以x=[1;3.5]和[1;7]来预测利润,得到theta

利润为

拟合曲线如下图:

两个变量:θ0 , θ1,因此画出来的图形可用三维空间来表示。

 

posted @ 2017-07-19 15:43  等候鸟飞回来  阅读(789)  评论(0编辑  收藏  举报