Skip to content

Commit

Permalink
First part of assignment 2
Browse files Browse the repository at this point in the history
  • Loading branch information
airhorns committed Nov 7, 2011
1 parent 98d84c9 commit 53dae97
Show file tree
Hide file tree
Showing 19 changed files with 1,394 additions and 44 deletions.
Binary file added ex2.pdf
Binary file not shown.
7 changes: 3 additions & 4 deletions mlclass-ex1/computeCostMulti.m
Expand Up @@ -6,16 +6,15 @@
% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly
% 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.




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

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

Expand Down
33 changes: 18 additions & 15 deletions mlclass-ex1/ex1_multi.m
Expand Up @@ -3,11 +3,11 @@
%
% Instructions
% ------------
%
%
% This file contains code that helps you get started on the
% linear regression exercise.
% linear regression exercise.
%
% You will need to complete the following functions in this
% You will need to complete the following functions in this
% exericse:
%
% warmUpExercise.m
Expand Down Expand Up @@ -43,8 +43,8 @@
fprintf('First 10 examples from the dataset: \n');
fprintf(' x = [%.0f %.0f], y = %.0f \n', [X(1:10,:) y(1:10,:)]');

fprintf('Program paused. Press enter to continue.\n');
pause;
%fprintf('Program paused. Press enter to continue.\n');
%pause;

% Scale features and set them to zero mean
fprintf('Normalizing Features ...\n');
Expand All @@ -54,19 +54,18 @@
% Add intercept term to X
X = [ones(m, 1) X];


%% ================ Part 2: Gradient Descent ================

% ====================== YOUR CODE HERE ======================
% Instructions: We have provided you with the following starter
% code that runs gradient descent with a particular
% learning rate (alpha).
% learning rate (alpha).
%
% Your task is to first make sure that your functions -
% computeCost and gradientDescent already work with
% Your task is to first make sure that your functions -
% computeCost and gradientDescent already work with
% this starter code and support multiple variables.
%
% After that, try running gradient descent with
% After that, try running gradient descent with
% different values of alpha and see which one gives
% you the best result.
%
Expand All @@ -85,7 +84,7 @@
alpha = 0.01;
num_iters = 100;

% Init Theta and Run Gradient Descent
% Init Theta and Run Gradient Descent
theta = zeros(3, 1);
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);

Expand All @@ -104,7 +103,11 @@
% ====================== 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

Xi = ([1650 3] .- mu) ./ sigma;
Xi = [ones(1) Xi];

price = Xi * theta; % You should change this


% ============================================================
Expand All @@ -120,12 +123,12 @@
fprintf('Solving with normal equations...\n');

% ====================== YOUR CODE HERE ======================
% Instructions: The following code computes the closed form
% Instructions: The following code computes the closed form
% solution for linear regression using the normal
% equations. You should complete the code in
% equations. You should complete the code in
% normalEqn.m
%
% After doing so, you should complete this code
% After doing so, you should complete this code
% to predict the price of a 1650 sq-ft, 3 br house.
%

Expand Down
28 changes: 14 additions & 14 deletions mlclass-ex1/featureNormalize.m
@@ -1,5 +1,5 @@
function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in 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
Expand All @@ -13,26 +13,26 @@
% ====================== 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
% 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.
% 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.
% 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.
%






%

for i = 1:size(X,2);
mu(i) = mean(X(:,i));
sigma(i) = std(X(:,i));
end;

X_norm = bsxfun(@minus, X, mu);
X_norm = bsxfun(@rdivide, X_norm, sigma);

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

Expand Down
16 changes: 5 additions & 11 deletions mlclass-ex1/gradientDescentMulti.m
Expand Up @@ -11,25 +11,19 @@

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










h = (X * theta);
delta = ((h .- y)' * X)';
theta = theta .- ((alpha / m) * delta);

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

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

end
Expand Down
Binary file modified mlclass-ex1/octave-core
Binary file not shown.
31 changes: 31 additions & 0 deletions mlclass-ex2/costFunction.m
@@ -0,0 +1,31 @@
function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
% parameter for logistic regression and the gradient of the cost
% w.r.t. to the parameters.

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

% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%

hypothesis = sigmoid(X * theta);
err = (-1 .* y .* log(hypothesis)) - ((1 .- y) .* log(1 .- hypothesis));
J = 1 / m * sum(err);

grad = (1 / m) * ((hypothesis - y)' * X)';

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

end
27 changes: 27 additions & 0 deletions mlclass-ex2/costFunctionReg.m
@@ -0,0 +1,27 @@
function [J, grad] = costFunctionReg(theta, X, y, lambda)
%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
% theta as the parameter for regularized logistic regression and the
% gradient of the cost w.r.t. to the parameters.

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

% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta






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

end
135 changes: 135 additions & 0 deletions mlclass-ex2/ex2.m
@@ -0,0 +1,135 @@
%% Machine Learning Online Class - Exercise 2: Logistic Regression
%
% Instructions
% ------------
%
% This file contains code that helps you get started on the logistic
% regression exercise. You will need to complete the following functions
% in this exericse:
%
% sigmoid.m
% costFunction.m
% predict.m
% costFunctionReg.m
%
% For this exercise, you will not need to change any code in this file,
% or any other files other than those mentioned above.
%

%% Initialization
clear ; close all; clc

%% Load Data
% The first two columns contains the exam scores and the third column
% contains the label.

data = load('ex2data1.txt');
X = data(:, [1, 2]); y = data(:, 3);

%% ==================== Part 1: Plotting ====================
% We start the exercise by first plotting the data to understand the
% the problem we are working with.

%fprintf(['Plotting data with + indicating (y = 1) examples and o ' ...
%'indicating (y = 0) examples.\n']);

%plotData(X, y);

%% Put some labels
%hold on;
%% Labels and Legend
%xlabel('Exam 1 score')
%ylabel('Exam 2 score')

%% Specified in plot order
%legend('Admitted', 'Not admitted')
%hold off;

%fprintf('\nProgram paused. Press enter to continue.\n');
%pause;


%% ============ Part 2: Compute Cost and Gradient ============
% In this part of the exercise, you will implement the cost and gradient
% for logistic regression. You neeed to complete the code in
% costFunction.m

% Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(X);

% Add intercept term to x and X_test
X = [ones(m, 1) X];

% Initialize fitting parameters
initial_theta = zeros(n + 1, 1);

% Compute and display initial cost and gradient
[cost, grad] = costFunction(initial_theta, X, y);

fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);

%fprintf('\nProgram paused. Press enter to continue.\n');
%pause;


%% ============= Part 3: Optimizing using fminunc =============
% In this exercise, you will use a built-in function (fminunc) to find the
% optimal parameters theta.

% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);

% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

% Print theta to screen
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf('theta: \n');
fprintf(' %f \n', theta);

%Plot Boundary
%plotDecisionBoundary(theta, X, y);

%Put some labels
%hold on;
%Labels and Legend
%xlabel('Exam 1 score')
%ylabel('Exam 2 score')

%Specified in plot order
%legend('Admitted', 'Not admitted')
%hold off;

%fprintf('\nProgram paused. Press enter to continue.\n');
%pause;

%% ============== Part 4: Predict and Accuracies ==============
% After learning the parameters, you'll like to use it to predict the outcomes
% on unseen data. In this part, you will use the logistic regression model
% to predict the probability that a student with score 20 on exam 1 and
% score 80 on exam 2 will be admitted.
%
% Furthermore, you will compute the training and test set accuracies of
% our model.
%
% Your task is to complete the code in predict.m

% Predict probability for a student with score 45 on exam 1
% and score 85 on exam 2

prob = sigmoid([1 45 85] * theta);
fprintf(['For a student with scores 45 and 85, we predict an admission ' ...
'probability of %f\n\n'], prob);

% Compute accuracy on our training set
p = predict(theta, X);

fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);

%fprintf('\nProgram paused. Press enter to continue.\n');
%pause;

0 comments on commit 53dae97

Please sign in to comment.