單變量線性迴歸算法
一、問題描述
分析單變量(城市人口)影響因素下,線性迴歸問題(快餐車利潤)。
二、概要
1.假設函數
2.代價函數:
3.訓練方法: 梯度下降法
三、代碼實現(.m)
1.主文件
ex1.m
clear ; close all; clc
%% ================ Part 1: Graphic Display1 ================
% Load Data
data = load('files\ex1data1.txt');
X = data(:,1);
y = data(:,2);
% Plot Training set data
subplot(2,2,1); plot(X,y,'rx','MarkerSize',10);
xlabel('Population of City (10,000)'); ylabel('Profit of Food Trucks ($10,000)');
%% ================ Part 2: Gradient Descent ================
% Choose some alpha value
alpha = 0.01;
num_iteration = 1500;
m = length(y);
% Init Theta and Run Gradient Descent
x = [ones(m,1),X];
theta = zeros(2,1);
[theta_group, J_group] = gradientDescent(x,y,theta,alpha,num_iteration);
% The trained Hypothetical Function
h = x*theta_group(:,num_iteration);
hold on;
plot(x(:,2),h,'b','LineWidth',2);
legend('Training set','Line Regression','Location','SouthEast');
%% ================ Part 3: Graphic Display2 ================
% Plot Loss during training
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)
t = [theta0_vals(i);theta1_vals(j)];
J_vals(i,j) = computeCost(x, y, t);
end
end
% Need to exchange coordinates due to surf & contour in Matlab
J_vals = J_vals';
%% ================ Part 4: Graphic Display3 ================
hold off;
subplot(2,2,2); surf(theta0_vals,theta1_vals,J_vals);
xlabel('\theta_0'); ylabel('\theta_1');title('3D surface');
subplot(2,2,3); contour(theta0_vals,theta1_vals,J_vals,logspace(-2, 3, 20));
xlabel('\theta_0'); ylabel('\theta_1');title('Contour map');
hold on;
plot(theta_group(1,num_iteration),theta_group(2,num_iteration),'rx','MarkerSize',10,'LineWidth', 2);
plot(theta_group(1,:),theta_group(2,:),'k.');
subplot(2,2,4);plot((1:num_iteration),J_group,'b','LineWidth',1.5);
xlabel('Training times'); ylabel('Loss');
2.調用函數
computeCost.m
function J = computeCost(x,y,theta)
%Number of Examples
m = length(y);
%Hypothetical Function
h = x * theta;
%Loss Function
J = (h-y)' * (h-y) / (2*m);
gradientDescent.m
function [theta_group,J_group] = gradientDescent(x,y,theta,alpha,num_iteration)
%Number of Features & Examples
num_feature = size(x,2);
m = length(y);
%Define theta_group & J_group
theta_group = zeros(num_feature,num_iteration);
J_group = zeros(num_iteration,1);
%Init theta_group & J_group
theta_group(:,1) = theta;
J_group(1) = computeCost(x,y,theta);
for i = 2:num_iteration
%Hypothetical Function
h = x * theta_group(:,i-1);
%Gradient Descent
for j = 1:num_feature
theta_group(j,i) = theta_group(j,i-1) - alpha * (h - y)'*x(:,j) / m;
end
%Loss Function
J_group(i) = computeCost(x,y,theta_group(:,i));
end
3.運行結果
四、代碼下載
鏈接:https://pan.baidu.com/s/1fguoYy2o1j4JXykz55NCgg
提取碼:27eb