单词学习
linear regression 线性回归
gradient descent 梯度下降
supervised learning problem 监督学习问题
iterations 迭代
converges 收敛
sanity-check 完整性检查
1 下载数据包
数据链接地址ex2Data.zip
给出的是50组2-8岁小男孩的体重数据,y为身高数据,X为相应的年纪。
本题给出的是50个数据样本点,其中x为这50个小朋友到的年龄,年龄为2岁到8岁,年龄可有小数形式呈现。Y为这50个小朋友对应的身高,当然也是小数形式表示的。现在的问题是要根据这50个训练样本,估计出3.5岁和7岁时小孩子的身高。通过画出训练样本点的分布凭直觉可以发现这是一个典型的线性回归问题。
2 算法实现
加载数据
x = load('ex2x.dat');
y = load('ex2y.dat');
画出数据图
figure % open a new figure window
plot(x, y, 'o');
ylabel('Height in meters')
xlabel('Age in years')
添加一个什么什么的
m = length(y); % store the number of training examples
x = [ones(m, 1), x]; % Add a column of ones to x
线性回归
线性回归模型
批量梯度下降更新规则
1.应用梯度下降,学习率α=0.07.在MATLAB中使用theta(1)和theta(2)来初始化。
2.继续执行梯度下降,直至theta收敛,记录最后theta(1)和theta(2)的值。
打印新的theta的值在原图上
hold on % Plot new data without clearing old plot
plot(x(:,2), x*theta, '-') % remember that x is now a matrix with 2 columns
% and the second column contains the time info
legend('Training data', 'Linear regression')
完整代码,源代码tornadomeet
在这里插入代码片