clear ; close all; clc
fprintf('Loading data ...n');
%% Load Data
data = load('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);
% Print out some data points
fprintf('First 10 examples from the dataset: n');
fprintf(' x = [%.0f %.0f], y = %.0f n', [X(1:10,:) y(1:10,:)]');
fprintf('Program paused. Press enter to continue.n');
pause;
% Scale features and set them to zero mean
fprintf('Normalizing Features ...n');
[X ,mu ,sigma] = featureNormalize(X);
% Add intercept term to X
X = [ones(m, 1) X];
%% ================ Part 2: Gradient Descent ================
% ====================== YOUR CODE HERE ======================
% Instructions: We have provided you with the following starter
% code that runs gradient descent with a particular
% learning rate (alpha).
%
% Your task is to first make sure that your functions -
% computeCost and gradientDescent already work with
% this starter code and support multiple variables.
%
% After that, try running gradient descent with
% different values of alpha and see which one gives
% you the best result.
%
% Finally, you should complete the code at the end
% to predict the price of a 1650 sq-ft, 3 br house.
%
% Hint: By using the 'hold on' command, you can plot multiple
% graphs on the same figure.
%
% Hint: At prediction, make sure you do the same feature normalization.
%
fprintf('Running gradient descent ...n');
% Choose some alpha value
alpha = [0.03,0.1,0.01];
num_iters = 30;
% Init Theta and Run Gradient Descent
theta = zeros(3, 1);
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
% Plot the convergence graph
figure;
plot(1:numel(J_history(:,1)), J_history(:,1), '-b', 'LineWidth', 2);
hold on
plot(1:numel(J_history(:,2)), J_history(:,2), '-r', 'LineWidth', 2);
hold on
plot(1:numel(J_history(:,3)), J_history(:,3), '-g', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');
% Display gradient descent's result
fprintf('Theta computed from gradient descent: n');
fprintf(' %f n', theta);
fprintf('n');
% Estimate the price of a 1650 sq-ft, 3 br house
% ====================== YOUR CODE HERE ======================
% Recall that the first column of X is all-ones. Thus, it does
% not need to be normalized.
U = [1650,3]; % You should change this
for i = 1:2
U(i)=(U(i)-mu(i))/sigma(i);
end
U = [1 U];
price = U*theta;
% ============================================================
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
'(using gradient descent):n $%fn'], price);
fprintf('Program paused. Press enter to continue.n');
pause;
%% ================ Part 3: Normal Equations ================
fprintf('Solving with normal equations...n');
首先是特征缩放函数featureNormalize
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = X;
mu = mean(X);
sigma = std(X);
for i = 1:2
X_norm(:,i) = (X(:,i)-mu(i))./sigma(i);
end
end
均值
标准差
其次是损失函数cost Jfunction J = computeCostMulti(X, y, theta)
m = length(y);
J = (X*theta-y)'*(X*theta-y);
end
最后是梯度下降Gradient decent
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
m = length(y);
J_history = zeros(num_iters, 3);
for iter = 1:num_iters
for i = 1:3
theta = theta-X'*(X*theta-y)*alpha(i)/m;
J_history(iter,i) = computeCostMulti(X, y, theta);
end
end
end
此处我设置了三个学习率,直接在函数中一次计算。
结果可以看到梯度下降效果明显
如有错误,欢迎批评指正


