【吴恩达 机器学习】ex1: gradient descend——MATLAB

该博客介绍了如何使用MATLAB进行单变量和多变量线性回归的实现。通过加载数据,绘制散点图,执行梯度下降算法求解最优参数,并进行预测。同时,展示了成本函数的三维图像和等高线图,帮助理解模型拟合效果。此外,还给出了不同人口数量下的利润预测值。
摘要由CSDN通过智能技术生成

单变量线性回归

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;

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值