Coursera-Machine Learning-ex1

本文探讨了在特征尺度相差悬殊时,如何通过特征缩放使梯度下降算法更快收敛。介绍了从数据集中减去每个特征的平均值,并将其特征值除以各自的标准差的方法。讨论了在调用特征规范化函数时,尚未向X添加额外的1对应于x0=1的列。强调了在规范化特征时存储用于规范化计算的平均值和标准差的重要性。在学习完模型参数后,预测未见过的房屋价格时,必须首先使用之前从训练集中计算得出的平均值和标准差对新x值进行规范化。文章还提供了计算成本、特征规范化、梯度下降和正规方程的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Some Points:

  1. When features differ by orders of magnitude, first performing feature scaling can make gradient descent converge much more quickly. DO: Subtract the mean value of each feature from the dataset, then divide the feature values by their respective "standard deviations".
  2. At the time that featureNormalize.m is called, the extra column of 1's corresponding to x0 = 1 has not yet been added to X.
  3. When normalizing the features, it is important to store the values used for normalization - the mean value and the standard deviation used for the computations. After learning the parameters from the model, we often want to predict the prices of houses we have not seen before. Given a new x value (living room area and number of bedrooms), we must first normalize x using the mean and standard deviation that we had previously computed from the training set.

The Results:

  • computeCost / computeCostMulti:
diff = X * theta - y;
J = diff' * diff / (2 * m);
  • featureNormalize:
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));

for i = 1 : size(X, 2)
    mu(i) = mean(X(:, i));
    sigma(i) = std(X(:, i));
    X_norm(:, i) = (X(:, i) - mu(i)) / sigma(i);
end
  • gradientDescent/ gradientDescentMulti:
% 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.
    %

    diff = X * theta - y;
    theta = theta - alpha / m * (X' * diff);

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

    % Save the cost J in every iteration    
    J_history(iter) = computeCostMulti(X, y, theta);
end
  • normalEqn:
theta = pinv(X' * X) * X' * y;
  • ex1_multi:
%% ================ Part 2: Gradient Descent ================
% Estimate the price of a 1650 sq-ft, 3 br house
% ====================== YOUR CODE HERE ======================
% Recall that the first column of X is all-ones. Thus, it does
% not need to be normalized.
price = 0; % You should change this
S = [1650, 3];
S = (S - mu) ./ sigma;
price = [1, S] * theta;
% ============================================================

fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
         '(using gradient descent):\n $%f\n'], price);

%% ================ Part 3: Normal Equations ================
% Estimate the price of a 1650 sq-ft, 3 br house
% ====================== YOUR CODE HERE ======================
price = 0; % You should change this

price = [1, 1650, 3] * theta;
% ============================================================

fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
         '(using normal equations):\n $%f\n'], price);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值