首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >吴恩达机器学习 - 单变量线性回归习题

吴恩达机器学习 - 单变量线性回归习题

作者头像
FishWang
发布2025-08-27 12:30:35
发布2025-08-27 12:30:35
9900
代码可运行
举报
运行总次数:0
代码可运行

题目链接:点击打开链接

这篇博客只是实现了单变量线性回归,多变量的内容在下一篇博客中展现。

首先表述下文符号的含义

m = 训练样本的数量 x = 输入变量/特征 y = 输出变量/目标变量 (x,y) = 训练样本

(x^{(i)},y^{(i)})

= 第i个样本

首先展示几个学到的公式:
  • 1.假设函数
h_{θ}(x)=θ_{0}+θ_{1}x
  • 2.代价函数
J(θ_{0},θ_{1})=\frac{1}{2m}
  • 3.梯度下降法
θ_{j}:=θ_{j}-α\frac{1}{m}\sum_{i=1}^{m}(h_{θ}(x^{i})-y^{(i)})*x^{(i)}

注意:这里要实现同步更新


然后是代码:

computeCost.m
代码语言:javascript
代码运行次数:0
运行
复制
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
%   J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
%   parameter for linear regression to fit the data points in X and y

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly 
J = 0;

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
%               You should set J to the cost.

%这个方法计算不好
%temp = X * theta;
%temp = temp - y;
%temp = temp.^2;
%J = sum(temp)/2.0/m;

%可以优化成这样
temp = X*theta-y;
J = temp'*temp/2.0/m;

% =========================================================================

end
plotData.m
代码语言:javascript
代码运行次数:0
运行
复制
function plotData(x, y)
%PLOTDATA Plots the data points x and y into a new figure 
%   PLOTDATA(x,y) plots the data points and gives the figure axes labels of
%   population and profit.

figure; % open a new figure window

% ====================== YOUR CODE HERE ======================
% Instructions: Plot the training data into a figure using the 
%               "figure" and "plot" commands. Set the axes labels using
%               the "xlabel" and "ylabel" commands. Assume the 
%               population and revenue data have been passed in
%               as the x and y arguments of this function.
%
% Hint: You can use the 'rx' option with plot to have the markers
%       appear as red crosses. Furthermore, you can make the
%       markers larger by using plot(..., 'rx', 'MarkerSize', 10);

plot(x, y, 'rx', 'MarkerSize', 10);  % Plot the data
ylabel('Profit in $10,000s');        % Set the y−axis label
xlabel('Population of City in 10,000s');     % Set the x−axis label

% ============================================================

end
gradientDescent.m
代码语言:javascript
代码运行次数:0
运行
复制
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
%   theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by 
%   taking num_iters gradient steps with learning rate alpha

% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);

for iter = 1:num_iters

    % ====================== YOUR CODE HERE ======================
    % Instructions: Perform a single gradient step on the parameter vector
    %               theta. 
    %
    % Hint: While debugging, it can be useful to print out the values
    %       of the cost function (computeCost) and gradient here.
    %

    theta = theta - alpha/m*X'*(X*theta - y);       %向量化计算更快


    % ============================================================

    % Save the cost J in every iteration    
    J_history(iter) = computeCost(X, y, theta);

end

end

最后运行一下ex1.m验证代码:

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 首先表述下文符号的含义
  • 首先展示几个学到的公式:
    • computeCost.m
    • plotData.m
    • gradientDescent.m
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档