Skip to content

Instantly share code, notes, and snippets.

@egonSchiele
Created March 10, 2016 03:54

Revisions

  1. egonSchiele created this gist Mar 10, 2016.
    68 changes: 68 additions & 0 deletions logistic_regression_grapefruit.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    % data
    x = [1, 2, 3, 4, 5, 6];
    y = [0, 0, 0, 1, 1, 1];

    % function to calculate the predicted value
    function result = h(x, t0, t1)
    result = sigmoid(t0 + t1 * x);
    end

    % sigmoid function
    function result = sigmoid(z)
    result = 1 / (1 + e ^ (-z));
    end

    % given a theta_0 and theta_1, this function calculates
    % their cost. We don't need this function, strictly speaking...
    % but it is nice to print out the costs as gradient descent iterates.
    % We should see the cost go down every time the values of theta get updated.
    function distance = cost(theta_0, theta_1, x, y)
    distance = 0;
    for i = 1:length(x) % arrays in octave are indexed starting at 1
    if (y(i) == 1)
    distance += -log(h(x(i), theta_0, theta_1));
    else
    distance += -log(1 - h(x(i), theta_0, theta_1));
    end
    end
    % get how far off we were on average
    distance = distance / length(x);
    end

    alpha = 0.1;
    iters = 1500;
    m = length(x);

    % initial values
    theta_0 = 0;
    theta_1 = 0;

    for i = 1:iters
    % we store this calculation in temporary variables,
    % because theta_0 and theta_1 must be updated *together*.
    % i.e. we can't update theta_0 and use the new theta_0 value
    % while updating theta_1.
    cost_0 = 0;
    for i = 1:m
    cost_0 += (h(x(i), theta_0, theta_1) - y(i));
    end
    temp_0 = theta_0 - alpha * 1/m * cost_0;

    cost_1 = 0;
    for i = 1:m
    cost_1 += ((h(x(i), theta_0, theta_1) - y(i)) * x(i));
    end
    temp_1 = theta_1 - alpha * 1/m * cost_1;

    theta_0 = temp_0;
    theta_1 = temp_1;

    % print out the new values of theta for each iteration,
    % as well as the new, lower cost
    disp([theta_0, theta_1, cost(theta_0, theta_1, x, y)]);
    end

    % print out predictions for numbers 1 to 10
    for i = 1:10
    disp([i, h(i, theta_0, theta_1) * 100]);
    end