看了很多的线性回归代码,感觉都没有写出算法的核心来,因此重新写了线性回归代码。下面的代码中缺少迭代退出机制,也就是 abs(ypredict - yobserver) < eps, 如果满足这个条件,就是求出了最优的theta值,就不需要继续迭代了。
还有问题,就是如何理解等高线的梯度下降?这个欢迎大家讨论。
代码见下:
% Exercise 2 Linear Regression
% Data is roughly based on 2000 CDC growth figures
% for boys
%
% x refers to a boy's age
% y is a boy's height in meters
%
clear all; close all; clc
x = load('ex2x.dat'); y = load('ex2y.dat');
m = length(y); % number of training examples
% Plot the training data
figure; % open a new figure window
plot(x, y, 'o');
ylabel('Height in meters')
xlabel(