machine-learning第二周 review中提交 Linear Regression 上机作业相关问题

环境:windows7


一、使用matlab

1、一开始使用matlab,但在提交时发现ex1/lib/makeValidFieldName.m 的18行应该为if(isoct),否则程序都无法跑通。

2、然后又碰到怎么都无法提交,总是提示

Submission failed: unexpected error: Error using ==> urlread
Could not POST to URL.


问题解决详见https://www.coursera.org/learn/machine-learning/discussions/vgCyrQoMEeWv5yIAC00Eog

注意到Tom Mosher 有这么一句话:

As with most open-source projects, the Octave team doesn't really focus on Windows. I don't have any problems with the Octave 4.0.0 Command Line Interface, so I usually stick with that. Sometimes the GUI acts a little weird, so I don't use it much unless I need to copy-and-paste a result into a Forum thread.

这给了我灵感,也许matlab也是不怎么care windows,因此我转而下载octave (什么鬼逻辑,上文明明说octave 不care windows


二、使用Octave4.0.0

1、提交时出现CA certificate 之类的错误,然后打了两个patch(不要再去修改上述makeValidFieldName.m的代码,用patch覆盖即可):

https://drive.google.com/file/d/0B6lXyE7fgSlXZjlqQ3FIRExmTDA/view?usp=sharing

https://drive.google.com/file/d/0B6lXyE7fgSlXXy1nMXlpb3RyZ1E/view?usp=sharing

2、重启Octave。 终于Done!

==
==                                   Part Name |     Score | Feedback
==                                   --------- |     ----- | --------
==                            Warm-up Exercise |  10 /  10 | Nice work!
==           Computing Cost (for One Variable) |  40 /  40 | Nice work!
==         Gradient Descent (for One Variable) |  50 /  50 | Nice work!
==                       Feature Normalization |   0 /   0 | Nice work!
==     Computing Cost (for Multiple Variables) |   0 /   0 | Nice work!
==   Gradient Descent (for Multiple Variables) |   0 /   0 | Nice work!
==                            Normal Equations |   0 /   0 | Nice work!
==                                   --------------------------------
==                                             | 100 / 100 |



三、总结:

1、请使用Octave上机,即使你修复了程序bug,在matlab也无法提交。

2、有问题先找讨论组:https://www.coursera.org/learn/machine-learning/discussions,有众多大神贴心解答。

3、作业我前前后后整了一周多,明白只有真正自己做出来的人才知道收获多少,因此不要轻易去看“答案”哦


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.


J = (1/(2*m))* sum((X * theta - y).^2);


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

end

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.



J = (1/(2*m))* (X * theta - y)' * (X * theta - y);


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

end

function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X 
%   FEATURENORMALIZE(X) returns a normalized version of X where
%   the mean value of each feature is 0 and the standard deviation
%   is 1. This is often a good preprocessing step to do when
%   working with learning algorithms.

% You need to set these values correctly
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));

% ====================== YOUR CODE HERE ======================
% Instructions: First, for each feature dimension, compute the mean
%               of the feature and subtract it from the dataset,
%               storing the mean value in mu. Next, compute the 
%               standard deviation of each feature and divide
%               each feature by it's standard deviation, storing
%               the standard deviation in sigma. 
%
%               Note that X is a matrix where each column is a 
%               feature and each row is an example. You need 
%               to perform the normalization separately for 
%               each feature. 
%
% Hint: You might find the 'mean' and 'std' functions useful.
%       
%disp(X);
mu = mean(X);
 %fprintf('%f %f \n', mu(1), mu(2));
sigma = std(X);
 %fprintf('%f %f \n', sigma(1), sigma(2));
n = size(X, 2);

for iter = 1:n
    X_norm(:,iter) =( X_norm(:,iter) - mu(iter) ) / sigma(iter)  ;
end

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

end

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
%   theta = GRADIENTDESENT(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.
    %

    % answer1:
    %theta(1) = theta(1) - (alpha * (1/m)) * sum((X*theta - y) ) ; 
    %theta(2) = theta(2) - (alpha * (1/m)) * (X(:,2)' * (X*theta - y)) ; 
    
    % answer2:
    delta = ((theta' * X' - y')*X)';
    theta = theta - alpha / m * delta;

    %fprintf('%f %f \n', theta(1), theta(2));


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

    % Save the cost J in every iteration    
    J_history(iter) = computeCost(X, y, theta);
    fprintf('%f \n', J_history(iter));

end

end

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);
 fprintf('X =\n');
disp(X);
 fprintf('y =\n');
 disp(y);
 
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.
    %



    %answer1 :
    %
    %tmp_status = (X * theta - y);
    %theta  = theta - (alpha * (1/m)) * ( X' * tmp_status ) ;
    %
    
    %answer2 :
    %
    %tmp_status = (X * theta - y);
    %theta(1) = theta(1) - (alpha * (1/m)) * sum(tmp_status ) ; 
    %theta(2) = theta(2) - (alpha * (1/m)) * (X(:,2)' * tmp_status) ; 
    %theta(3) = theta(3) - (alpha * (1/m)) * (X(:,3)' * tmp_status) ; 
    %
     
    %answer3 :
    delta = ((theta' * X' - y')*X)';
    theta = theta - alpha / m * delta;
    
      
    %fprintf('%f %f %f\n', theta(1), theta(2),theta(3));
     


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

    % Save the cost J in every iteration    
    J_history(iter) = computeCostMulti(X, y, theta);
    fprintf('%f \n', J_history(iter));
end

end

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 = (inv( X' * X )) * X' * y ;


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


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

end


你可能感兴趣的:(机器学习)