Andrew Ng coursera上的《机器学习》ex1
本系列文章是在coursera上学习Andrew Ng的《机器学习》之后,对练习题进行了一些总结。我是初学者,所以肯定存在很多错误,欢迎大家能够给我提意见。
按照课程所给的ex1的文档要求,ex1要求完成以下几个计算过程的代码编写:
exerciseName | description |
---|---|
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 |
|
1. warmUpExercise.m
要求通过写Octave/MATLAB代码返回一个5阶的单位矩阵。
X = eye(5);
2. plotData.m
要求将二维的训练数据的x和y用图展示出来。
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.
% ====================== 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);
figure; % open a new figure window
plot(x,y,'rx','MarkerSize',10);
ylabel('Profit in $10,000s');
xlabel('Population of City in $10,000s');
% ============================================================
end
其中传入的参数X,Y分别用下面的代码求出:
data = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples
3. computeCost.m
要求:计算出线性回归函数中对应于只有一个特征值(X是二维的)的情况进行计算。
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 = sum((X*theta-y).^2)/(2*m);
% =========================================================================
end
其中传入的参数分别为:
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;
其中的X是一个m*2的矩阵,theta是一个2 1的矩阵,所以m个数据集的h(x)= X theta,与之前描述的h(x) = (theta^T ) * X 有一定的区别,需要注意。这些是通过数学的推导得到的结果。
4.gradientDescent.m
利用批量梯度下降来对参数进行最优化求解。梯度下降的公式如下:
theta = theta - α * sum(h(x)-y)*x /m
其中的sum可以用求和符号表示。在Octave中,可以转为矩阵来进行计算。
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 print out the values
% of the cost function (computeCost) and gradient here.
%
theta = theta - alpha*X'*(X*theta-y)/m;
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
其中传入的参数:
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;