Coursera-Machine Learning-ex1

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);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值