最近开始看斯坦福的公开课《Machine Learning》,对其中单参数的Linear Regression(未涉及Gradient Descent)做个总结吧。
【设想】
最近想要租房,收集了一些信息,得知房价与房间大小有关,那成本函数就可以预测在不同房间大小下租房的价格(PS:价格可能也与该房地理有关,那若把大小和距离市中心距离一并考虑,则属于多参数的线性回归)
【数据】
1、准备一个ex1data1.txt,第一列为年龄,第二列为价格
2、导入matlab
data = load('ex1data1.txt');
3、赋值给不同的变量
X = data(:,1); %第一列年龄作为X
y = data(:,2); %第二列价格作为y
4、查看导入的数据
plot(X, y,'rx','MarkerSize',10);



我们需要找到一条y=a+bx的直线(a=theta0,b=theta1),最大程度的拟合上图中的点,那如何取得a与b的值,则运用了成本函数,如下:
【公式】

【MatLab】
data = load('ex1data1.txt');
y = data(:,2); %选取价格作为y变量
m = length(y);
X = [ones(m, 1), data(:,1)]; %选取房间大小作为第二列,第一列为1
theta0_vals = linspace(-3,3,100); %确定theta0轴的范围为[-3,3],100个刻度
theta1_vals = linspace(-1,1,100);
for i=1:length(theta0_vals) %循环执行computeCost函数,找到min(J_vals)的theta0和theta1值
for j=1:length(theta1_vals)
t = [theta0_vals(i); theta1_vals(j)];
J_vals(i,j) = computeCost(X, y, t);
end
end
% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');
% eg:X(i,:)= [1,6]
% eg:t= 

function J = computeCost(X, y, theta)
m = length(y); % 训练集个数
J = 0;
h=0;
for i= 1:m
h = h + (theta' * X(i,:)'-y(i))^2; %实现J(theta)公式的后半部
end
J = 1/(2*m)*h
end
eg:
,这就是为什么X变量要特地赋成两列,为了实现a+bx的矩阵效果

【可视化结果】

参考:
斯坦福机器学习的课后习题及程序:
下载
下载