以一个简单的例子来实现线性回归代码。
如题:
year = [2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013];
price =[2000,2500,2900,3147,4515,4903,5365,5704,6853,7971,8561,10000,11280,12900];
year和price分别表示年份和房价,拟合一条线性回归的函数来建立年份和房价的关系。
代码如下:
clear all;
close all;
clc
x1=[2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013];
% x=[0,1,2,3,4,5,6,7,8,9,10,11,12,13]';
y=[2000,2500,2900,3147,4515,4903,5365,5704,6853,7971,8561,10000,11280,12900];
m = length(y); % number of training examples
% Plot the training data
figure;
plot(x1, y, 'o');
ylabel('price')
xlabel('year')
% Gradient descent
%这里我将输入数据归一化处理的,因为直接用原数据SGD不能随机梯度下降;另外可以直接将年份减小也可行。
x=(x1-min(x1).*ones(size(x1)))./(max(x1)-min(x1));
theta = ones(size(x(:,1))+1,1); % initialize fitting parameters
MAX_ITR = 1500;
alpha = 0.002;
for num_iterations = 1:MAX_ITR
theta1 = (theta(1,1).*x+repmat(theta(2,1),size(y))-y)*x';
b = sum((theta(1,1).*x+repmat(theta(2,1),size(y))-y));
% Here is the actual update
theta = theta -alpha .* [theta1;b];
end
theta
hold on; % keep previous plot visible
plot_y = theta(1,1)*x+theta(2,1);
plot(x1, plot_y, '-')
legend('Training data', 'Linear regression')
hold off % don't overlay any more plots on this figure
贴一下结果: