-1

Running my gradient descent function against the training data produces thetas of [0.3157; 0.0176; 0.0148]. The first value is significantly higher then the others. When it comes to predicting the probability of my test data, ends up being 0.42 +- 0.01. Which makes the probability being closer to 0, always. I believe the error relies in the gradient descent function.

Gradient Descent Function

function [theta] = GradientDescent(x, y)
    m = size(x,1);  
    n = size(x,2);  

    theta = zeros(n,1);
    alpha = .005; 
    iterations = 10000; 
    J=[];

    for i = 1:iterations
        h = x * theta;
        theta = theta - (alpha/m)* x' * (h-y);
        J_old = J;
        J = -(1/m) * sum(y .* log(h) + (1 - y) .* log(1-h));
        if((i>2 && abs(J_old - J) < 10^-5))
             break;
        end
        if(any(isnan(theta())))
            disp("breaking the iterations since theta returns NaN values");
            break;
        end
    end
    disp("Performing Gradient descent -  with "+n+" features");
end

Main Code - Loading Data and running probability

[X, Y] = LoadData("train_q1.csv");
scatter(X(:, 2), X(:, 3), 25, Y);
% 1 is buy - on the ends
% 0 is sell - in the middle
%============ 1b.
thetas = ones(3, 1);
[theta] = GradientDescent(X, Y);
disp(theta);
% get accuracy
[trainX, trainY] = LoadData("test_q1.csv");

correct = 0;
%probability
for i=1:length(trainY)
    disp(1 ./ (1 + exp(trainX(i, :) * theta)));
    probability = round(1 ./ (1 + exp(trainX(i, :) * theta)));
    if trainY(i) == probability
        correct = correct + 1;
    end
end
disp(correct);
% print accuracy
disp("The model is " + (correct/length(trainY) * 100) + "% correct");

1 Answers1

0
  • You have used h = x * theta inside your GradientDescent(..) function to calculate your hypothesis function which is incorrect. It should be h = 1./(1 + exp(-x*theta))(Note the minus sign).
  • So when you're calculating probability, it should be disp(1 ./ (1 + exp(-(trainX(i, :) * theta)))). Note that you have not included the minus (-) sign inside exp().

  • Also in [trainX, trainY] = LoadData("test_q1.csv"), the names are incorrect. It should be testX, testY not trainX, trainY because you're loading a test data set (test_q1.csv).

thilakshiK
  • 735
  • 2
  • 6
  • 11