In [1]:
clear ; close all; clc

In [2]:
%% Initialization
%% Setup the parameters you will use for this part of the exercise
input_layer_size  = 400;  % 20x20 Input Images of Digits
num_labels = 10;          % 10 labels, from 1 to 10
                          % (note that we have mapped "0" to label 10)

In [3]:
% Load Training Data
fprintf('Loading and Visualizing Data ...\n')

load('./data/ex3data1.mat'); % training data stored in arrays X, y
m = size(X, 1);

% Randomly select 100 data points to display
rand_indices = randperm(m);
sel = X(rand_indices(1:100), :);

displayData(sel);


Loading and Visualizing Data ...

In [5]:
% Test case for lrCostFunction
fprintf('\nTesting lrCostFunction()');
theta_t = [-2; -1; 1; 2];
X_t = [ones(5,1) reshape(1:15,5,3)/10];
y_t = ([1;0;1;0;1] >= 0.5);
lambda_t = 3;
[J grad] = lrCostFunction(theta_t, X_t, y_t, lambda_t);

fprintf('\nCost: %f\n', J);
fprintf('Gradients:\n');
fprintf(' %f \n', grad);


Testing lrCostFunction()

Cost: 2.534819
Expected cost: 2.534819
Gradients:
 0.146561 
 -0.548558 
 0.724722 
 1.398003 
Expected gradients:
 0.146561
 -0.548558
 0.724722
 1.398003
Testing lrCostFunction()

In [7]:
fprintf('\nTraining One-vs-All Logistic Regression...\n')

lambda = 0.1;
[all_theta] = oneVsAll(X, y, num_labels, lambda);


Training One-vs-All Logistic Regression...
Iteration    50 | Cost: 1.394147e-02
Iteration    50 | Cost: 5.725251e-02
Iteration    50 | Cost: 6.390191e-02
Iteration    50 | Cost: 3.580225e-02
Iteration    50 | Cost: 6.181875e-02
Iteration    50 | Cost: 2.172477e-02
Iteration    50 | Cost: 3.541976e-02
Iteration    50 | Cost: 8.586671e-02
Iteration    50 | Cost: 7.983655e-02
Iteration    50 | Cost: 9.964966e-03

In [8]:
pred = predictOneVsAll(all_theta, X);

fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);


Training Set Accuracy: 95.020000

In [ ]: