编程作业: Linear Regression

这篇博客介绍了线性回归的基本概念,包括数据可视化、成本函数计算和梯度下降优化方法。通过plotData.m展示数据集,computeCost.m用于计算线性回归的成本,gradientDescent.m实现梯度下降算法。此外,还探讨了多元线性回归,使用computeCostMulti.m和gradientDescentMulti.m进行多变量情况下的成本计算和梯度下降。normalEqn.m则展示了使用正常方程求解线性回归参数的方法。
摘要由CSDN通过智能技术生成

plotData.m - Function to display the dataset

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

%(function plotData(x,y)
%PLOTDATA将数据点x和y绘制成新的数字
%PLOTDATA(x,y)绘制数据点并给出数字轴标签人口和利润百分比。)
figure; %打开一个新的数字窗口

% ====================== 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.
%(说明:使用数据将训练数据绘制成图形,使用“figure”和“plot”命令用来设置轴标签)
%(“xlabel”和“ylabel”命令用来命名X和Y轴, 假设人口和收入数据已作为此函数的x和y参数传入。)			 
% 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);
%(提示:可以在绘图中使用'rx'选项来标记数据,让它显示为红色十字。 此外,你可以
%使用plot(...,'rx','MarkerSize',10)设置标记的大小)

data = load('ex1data1.txt')  % 读取数据文件,需要先cd到目录
x = data(:, 1); y = data(:, 2); % data(:,1)就是取data的第一列
figure(1); plot(x, y, 'rx', 'MarkerSize', 10); % 创建一个数字窗口,绘制x和y,用rx标记(就是红色的x),标记的大小为10
xlabel('population') % 将x轴命名为'population'
ylabel('revenue data') % 将y轴命名为'revenue data'




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

end

computeCost.m - Function to compute the cost of linear regression

function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
%(COMPUTECOST 计算线性回归的代价)
%   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
%   (J = COMPUTECOST(X, y, theta) 使用theta作为计算线性回归的参数以适合X和y中的数据点的成本)

% Initialize some useful values
%(初始化一些有用的值)
m = length(y); % 训练样本的个数

% You need to return the following variables correctly 
%(你需要正确返回以下变量)
J = 0;

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
%(说明:计算特定选择的theta的代价)
%               You should set J to the cost.
%				(你应该将J设置为代价)


h = X * theta  % hθ(x)
J = 1/(2*m) * sum( (h - y) .^ 2) % 代价函数



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

end

gradientDescent.m - Function to run gradient descent

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
%(GRADIENTDESCENT执行梯度下降以学习theta)
%   theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by 
%   taking num_iters gradient steps with learning rate alpha
%( theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) 通过使用学习率alpha的num_iters梯度步骤更新theta)

% 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.
    %(提示:在调试时,可以在这里打印出代价函数(computecost)和梯度的值。)


predictions=X*theta;        
errors=(predictions-y);      
sums=X'*errors;              
delta=1/m *sums;

theta=theta-alpha*delta;




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

    % Save the cost J in every iteration    
    %(在每次迭代中保存代价J)
    J_history(iter) = computeCost(X, y, theta);

end

end

computeCostMulti.m - Cost function for multiple variables

function J = computeCostMulti(X, y, theta)
%COMPUTECOSTMULTI Compute cost for linear regression with multiple variables
%(多变量线性回归)
%   J = COMPUTECOSTMULTI(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.

%多变量和单变量并没有什么区别



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

end

gradientDescentMulti.m - Gradient descent for multiple variables

    function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
    %GRADIENTDESCENTMULTI Performs gradient descent to learn theta
    %   theta = GRADIENTDESCENTMULTI(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 (computeCostMulti) and gradient here.
        %
    
    
    
predictions=X*theta;        
errors=(predictions-y);      
sums=X'*errors;              
delta=1/m *sums;

theta=theta-alpha*delta;








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

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

end

end

normalEqn.m - Function to compute the normal equations

function [theta] = normalEqn(X, y)
%NORMALEQN Computes the closed-form solution to linear regression 
%   NORMALEQN(X,y) computes the closed-form solution to linear 
%   regression using the normal equations.

theta = zeros(size(X, 2), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the code to compute the closed form solution
%               to linear regression and put the result in theta.
%

% ---------------------- Sample Solution ----------------------

theta = pinv(X'*X)*X'*y


% -------------------------------------------------------------


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

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值