# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1319940190 18000 # Node ID 8b902ada47e9322c4b24400947721315b31254c7 # Parent 5664e0047b3efd33416c3de7735fc5d555c732b2 Complete part 1 diff --git a/costFunction.m b/costFunction.m --- a/costFunction.m +++ b/costFunction.m @@ -1,32 +1,15 @@ 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)); +##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. -% ====================== 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 -% + m = length (y); - - - + ## h_theta(x) + ht = sigmoid (X*theta); - - - -% ============================================================= + J = -sum (y.*log (ht) + (1 - y).*log (1 - ht))/m + grad = X'*(ht - y)/m; end diff --git a/plotData.m b/plotData.m --- a/plotData.m +++ b/plotData.m @@ -1,28 +1,18 @@ function plotData(X, y) -%PLOTDATA Plots the data points X and y into a new figure -% PLOTDATA(x,y) plots the data points with + for the positive examples -% and o for the negative examples. X is assumed to be a Mx2 matrix. +##PLOTDATA Plots the data points X and y into a new figure +## PLOTDATA(x,y) plots the data points with + for the positive examples +## and o for the negative examples. X is assumed to be a Mx2 matrix. -% Create New Figure +## Create New Figure figure; hold on; -% ====================== YOUR CODE HERE ====================== -% Instructions: Plot the positive and negative examples on a -% 2D plot, using the option 'k+' for the positive -% examples and 'ko' for the negative examples. -% - - - - - - - - - -% ========================================================================= - - +## Find Indices of Positive and Negative Examples +pos = find(y==1); neg = find(y == 0); +## Plot Examples +plot (X(pos, 1), X(pos, 2), "k+","LineWidth", 2, + "MarkerSize", 7); +plot (X(neg, 1), X(neg, 2), "ko", "MarkerFaceColor", + "y", "MarkerSize", 7); hold off; diff --git a/predict.m b/predict.m --- a/predict.m +++ b/predict.m @@ -1,27 +1,9 @@ function p = predict(theta, X) -%PREDICT Predict whether the label is 0 or 1 using learned logistic -%regression parameters theta -% p = PREDICT(theta, X) computes the predictions for X using a -% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1) - -m = size(X, 1); % Number of training examples - -% You need to return the following variables correctly -p = zeros(m, 1); +##PREDICT Predict whether the label is 0 or 1 using learned logistic +##regression parameters theta +## p = PREDICT(theta, X) computes the predictions for X using a +## threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1) -% ====================== YOUR CODE HERE ====================== -% Instructions: Complete the following code to make predictions using -% your learned logistic regression parameters. -% You should set p to a vector of 0's and 1's -% - - - - - - - -% ========================================================================= - + p = sigmoid (X*theta) >= 0.5; end diff --git a/sigmoid.m b/sigmoid.m --- a/sigmoid.m +++ b/sigmoid.m @@ -1,18 +1,7 @@ function g = sigmoid(z) -%SIGMOID Compute sigmoid functoon -% J = SIGMOID(z) computes the sigmoid of z. - -% You need to return the following variables correctly -g = zeros(size(z)); +##SIGMOID Compute sigmoid functoon +## J = SIGMOID(z) computes the sigmoid of z. -% ====================== YOUR CODE HERE ====================== -% Instructions: Compute the sigmoid of each value of z (z can be a matrix, -% vector or scalar). - - - - - -% ============================================================= + g = 1./(1 + exp (-z)); end