单变量线性回归
MATLAB代码:
% load the data
data = load("ex1data1.txt");
input = data(:, 1);
output = data(:, 2);
%scatter plot
plot(input, output, 'rx', 'Markersize', 10);
[r, c] = size(input);
X = [ones(r, 1), input];
X = X';
Y = output;
theta = zeros(2, 1);
theta_list = zeros(2, r);
alpha = 0.001; %learning rate
iter_cnt = 100000; %iterate number
% gradient decendent
for i = 1 : iter_cnt
aux1 = 0;
aux2 = 0;
for j = 1 : r
aux1 = aux1 + (theta' * X(:,j) - Y(j,1)) * X(1,j);
aux2 = aux2 + (theta' * X(:,j) - Y(j,1)) * X(2,j);
end
theta = theta - [aux1; aux2] * alpha/r;
theta_list(:,i) = theta;
end
% fitted curve
hold on;
plot(input, theta' * X, 'b-');
hold off;
%compute the cost for each theta0 and theta1
theta0_vals = linspace(-10, 10, 100);
theta1_vals = linspace(-1, 4, 100);
J_vals = zeros(length(theta0_vals), length(theta1_vals));
for i = 1:length(theta0_vals)
for j = 1:length(theta1_vals)
hyposis = [theta0_vals(i),theta1_vals(j)] * X - Y';
errors = hyposis * hyposis';
J_vals(i,j) = errors/(2*r);
end
end
J_vals = J_vals.';
% predict profit when population of 35,000 and 70,000
predict1 = [1, 3.5] * theta;
fprintf('For population = 35,000, we predict a profit of %f\n',...
predict1*10000);
predict2 = [1, 7] * theta;
fprintf('For population = 70,000, we predict a profit of %f\n',...
predict2*10000);
%Draw a three-dimensional image of the cost function J(theta_0, theta_1)
fprintf('Visualizing J(theta_0, theta_1) ...\n')
theta0_vals = linspace(-10, 10, 100);
theta1_vals = linspace(-1, 4, 100);
figure;
surf(theta0_vals,theta1_vals,J_vals);
% Draw contour lines---ellipse
figure;
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20),'ShowText','on');
xlabel('\theta_0');
ylabel('\theta_1');
hold on;
plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);
![在这里插入图片描述](https://img-blog.csdnimg.cn/aa223240e88a4eef963db7fc52b04b0f.png?x-oss-process=image/watermark,type_ZmFuZ3poZ
多变量线性回归
% load the data
data = load("ex1data2.txt");
input1 = data(:, 1) / 1000;
input2 = data(:, 2);
output = data(:, 3) / 100000;
% scatter plot
plot3(input1, input2, output, 'rx', 'Markersize', 10);
[r, c] = size(input1);
X = [ones(r, 1), input1, input2];
X = X';
Y = output;
theta = zeros(3, 1);
theta_list = zeros(3, r);
alpha = 0.00001; %learning rate
iter_cnt = 100000; %iterate number
% gradient decendent
% calculate theta and cost function while iteration
for i = 1 : iter_cnt
aux1 = 0;
aux2 = 0;
aux3 = 0;
for j = 1 : r
aux1 = aux1 + (theta' * X(:,j) - Y(j,1)) * X(1,j);
aux2 = aux2 + (theta' * X(:,j) - Y(j,1)) * X(2,j);
aux3 = aux3 + (theta' * X(:,j) - Y(j,1)) * X(3,j);
end
theta = theta - [aux1; aux2; aux3] * alpha/r;
theta_list(:,i) = theta;
end
% fitted curve
hold on;
plot3(input1, input2, theta' * X, 'b-');
hold off;