Skip to content

Instantly share code, notes, and snippets.

@ferbncode
Created October 28, 2017 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferbncode/5b1b4ce33cb406d292e076c3ca29ad10 to your computer and use it in GitHub Desktop.
Save ferbncode/5b1b4ce33cb406d292e076c3ca29ad10 to your computer and use it in GitHub Desktop.
function [all_theta] = oneVsAll(X, y, k, lambda)
m = size(X, 1);
n = size(X, 2);
alpha = 0.01;
% this is storing all the theta for all the individual problems
all_theta = zeros(k, n + 1); % 6 x (n+1)
X = [ones(m, 1) X];
for i=1:k
theta = zeros(n + 1, 1);
for j = 1:100
data = [y, X];
data = data(randperm(size(data,1)),:);
y = data(:,1);
y_final = zeros(size(y))
for p = 1:m
y_final(y(p)) = 1;
X = data(:,2:end);
for p = 1:m
x = X(p,:); % Select one example
Y = y_final(p)
[J, grad] = lrCostFunction(theta, x, Y, lambda);
theta = theta - alpha * grad;
end
end
% options = optimset('GradObj', 'on', 'MaxIter', 50);
% [theta] = fmincg (@(t)(lrCostFunction(t, X, (y == i), lambda)),initial_theta, options);
all_theta(i,:) = theta;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment