实验结果:
theta = [0.7502;0.0639]
程序代码及注释:
采用normal equations方法求解:
close all;clear;clc;
%导入数据并画出x,y的散点图
x = load('E:\csample\deeplearning\ex2Data\ex2x.dat');
y = load('E:\csample\deeplearning\ex2Data\ex2y.dat');
figure;
plot(x, y, 'o')
xlabel('Age in years');
ylabel('Height in meters');
%根据公式求解theta值
m = length(y);
x = [ones(m, 1), x];
theta = inv(x'*x)*x'*y;
%画出回归线
hold on;
plot(x(:,2), x * theta , '-');
legend('training data','Linear regression');
hold off;
采用gradient descend过程求解:
close all;clear;clc;
%导入数据并画出x,y的散点图
x = load('E:\csample\deeplearning\ex2Data\ex2x.dat');
y = load('E:\csample\deeplearning\ex2Data\ex2y.dat');
figure;
plot(x, y, 'o')
xlabel('Age in years');
ylabel('Height in meters');
%初始化参数theta、最大迭代次数和学习速率
m = length(y);
x = [ones(m, 1), x];
theta = zeros(size(x(1, :)))';
MAX_ITR = 1500;
alpha = 0.07;
%梯度下降法求解theta
for num_iteration = 1:MAX_ITR
grad = (1/m).* x' * ((x * theta) - y);
theta = theta - alpha .* grad;
end
%画出回归线
hold on;
plot(x(:,2), x * theta , '-');
legend('training data','Linear regression');
hold off;