Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
curve_fitting/cost_logisticV3.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
25 lines (21 sloc)
1.13 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [cost, gradient] = cost_logisticV3(a, x, y) | |
% cost_logisticV3 Compute cost and gradient for logistic regression | |
% cost = cost_logistic(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 ====================== | |
% Compute the cost of a particular choice of a. Compute the partial derivatives and set grad to the partial derivatives of the cost w.r.t. each parameter in theta | |
cost = 0; %initialize values | |
gradient = 0; %initalize a gradient | |
t = a(1).*(x.^0)+a(2).*x; %time function | |
sigma = 1./(1+exp(-t)); %calculate sigma | |
cost = sum(-y.*log(sigma)- (1-y).*log(1-sigma)); %Calculation of cost | |
costFunction = @ (a) sum(-y.*log((1./(1+exp(-(a(1)+a(2).*x)))))-(1-y).*log(1-(1./(1+exp(-(a(1)+a(2).*x)))))); | |
gradient = (1/length(x))*sum((sigma-y).*t); %Calculation of gradient | |
ai = [0 0]; %initialize a value for a | |
%---------------------- Plot of Regression ----------------------------- | |
plot(x,y,'x', x, sigma); | |
title('Regression') | |
xlabel('Temp (Degrees Fahrenheit)') | |
ylabel('Pass or Fail (1 or 0)') | |
end |