From fd8432ccbf7d2d88dc24ad592cd7ce7868102fac Mon Sep 17 00:00:00 2001 From: mjjo53 Date: Sun, 28 May 2017 06:50:45 +0900 Subject: [PATCH] =?UTF-8?q?=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex2/costFunction.m | 4 +++- ex2/costFunctionReg.m | 6 +++--- ex2/ex2_reg.m | 6 +++--- ex2/plotData.m | 9 +++++++-- ex2/predict.m | 3 ++- ex2/sigmoid.m | 2 +- ex2/token.mat | 15 +++++++++++++++ 7 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 ex2/token.mat diff --git a/ex2/costFunction.m b/ex2/costFunction.m index 1d98fbe..e67e39d 100644 --- a/ex2/costFunction.m +++ b/ex2/costFunction.m @@ -20,8 +20,10 @@ grad = zeros(size(theta)); % Note: grad should have the same dimensions as theta % +hx = sigmoid(X*theta); +J = sum(-y.*log(hx) - (1.-y).*log(1.-hx))/m; - +grad = sum((hx-y).*X)/m; diff --git a/ex2/costFunctionReg.m b/ex2/costFunctionReg.m index 87b7e8e..6472faa 100644 --- a/ex2/costFunctionReg.m +++ b/ex2/costFunctionReg.m @@ -18,9 +18,9 @@ grad = zeros(size(theta)); % derivatives of the cost w.r.t. each parameter in theta - - - +hx = sigmoid(X*theta); +J = sum(-y.*log(hx)-(1.-y).*log(1.-hx))/m + lambda*sum([0; theta(2:end)].^2)/(2*m); +grad = (sum((hx-y).*X)./m)' + lambda*[0; theta(2:end)]/m; % ============================================================= diff --git a/ex2/ex2_reg.m b/ex2/ex2_reg.m index f363318..ddada5b 100644 --- a/ex2/ex2_reg.m +++ b/ex2/ex2_reg.m @@ -75,7 +75,7 @@ fprintf('Expected gradients (approx) - first five values only:\n'); fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n'); fprintf('\nProgram paused. Press enter to continue.\n'); -pause; +%pause; % Compute and display cost and gradient % with all-ones theta and lambda = 10 @@ -90,7 +90,7 @@ fprintf('Expected gradients (approx) - first five values only:\n'); fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n'); fprintf('\nProgram paused. Press enter to continue.\n'); -pause; +%pause; %% ============= Part 2: Regularization and Accuracies ============= % Optional Exercise: @@ -107,7 +107,7 @@ pause; initial_theta = zeros(size(X, 2), 1); % Set regularization parameter lambda to 1 (you should vary this) -lambda = 1; +lambda = 100; % Set Options options = optimset('GradObj', 'on', 'MaxIter', 400); diff --git a/ex2/plotData.m b/ex2/plotData.m index 5df9d3f..569e4a8 100644 --- a/ex2/plotData.m +++ b/ex2/plotData.m @@ -12,10 +12,15 @@ figure; hold on; % examples and 'ko' for the negative examples. % +pos = find(y==1); +neg = find(y == 0); +plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, 'MarkerSize', 7); +plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7); - - +ylabel('Exam 2 score'); +xlabel('Exam 1 score'); +legend('Admitted', 'Not admitted'); diff --git a/ex2/predict.m b/ex2/predict.m index 34a2905..dc5aa17 100644 --- a/ex2/predict.m +++ b/ex2/predict.m @@ -17,7 +17,8 @@ p = zeros(m, 1); - +hx = X*theta; +p = hx >= 0; diff --git a/ex2/sigmoid.m b/ex2/sigmoid.m index 61cc9b1..688b145 100644 --- a/ex2/sigmoid.m +++ b/ex2/sigmoid.m @@ -10,7 +10,7 @@ g = zeros(size(z)); % vector or scalar). - +g = 1./(1+exp(-z)); % ============================================================= diff --git a/ex2/token.mat b/ex2/token.mat new file mode 100644 index 0000000..408cfce --- /dev/null +++ b/ex2/token.mat @@ -0,0 +1,15 @@ +# Created by Octave 4.2.1, Wed May 10 12:15:18 2017 GMT +# name: email +# type: sq_string +# elements: 1 +# length: 16 +mjjo53@gmail.com + + +# name: token +# type: sq_string +# elements: 1 +# length: 16 +fZMMhxjQUsSZ1osy + +