in Education by
I am learning Machine Learning course from coursera from Andrews Ng. I have written a code for logistic regression in octave. But, it is not working. Can someone help me? I have taken the dataset from the following link: Titanic survivors Here is my code: pkg load io; [An, Tn, Ra, limits] = xlsread("~/ML/ML Practice/dataset/train_and_test2.csv", "Sheet2", "A2:H1000"); # As per CSV file we are reading columns from 1 to 7. 8-th column is Survived, which is what we are going to predict X = [An(:, [1:7])]; Y = [An(:, 8)]; X = horzcat(ones(size(X,1), 1), X); # Initializing theta values as zero for all #theta = zeros(size(X,2),1); theta = [-3;1;1;-3;1;1;1;1]; learningRate = -0.00021; #learningRate = -0.00011; # Step 1: Calculate Hypothesis function g_z = estimateHypothesis(X, theta) z = theta' * X'; z = z'; e_z = -1 * power(2.72, z); denominator = 1.+e_z; g_z = 1./denominator; endfunction # Step 2: Calculate Cost function function cost = estimateCostFunction(hypothesis, Y) log_1 = log(hypothesis); log_2 = log(1.-hypothesis); y1 = Y; term_1 = y1.*log_1; y2 = 1.-Y; term_2 = y2.*log_2; cost = term_1 + term_2; cost = sum(cost); # no.of.rows m = size(Y, 1); cost = -1 * (cost/m); endfunction # Step 3: Using gradient descent I am updating theta values function updatedTheta = updateThetaValues(_X, _Y, _theta, _hypothesis, learningRate) #s1 = _X * _theta; #s2 = s1 - _Y; #s3 = _X' * s2; # no.of.rows #m = size(_Y, 1); #s4 = (learningRate * s3)/m; #updatedTheta = _theta - s4; s1 = _hypothesis - _Y; s2 = s1 .* _X; s3 = sum(s2); # no.of.rows m = size(_Y, 1); s4 = (learningRate * s3)/m; updatedTheta = _theta .- s4'; endfunction costVector = []; iterationVector = []; for i = 1:1000 # Step 1 hypothesis = estimateHypothesis(X, theta); #disp("hypothesis"); #disp(hypothesis); # Step 2 cost = estimateCostFunction(hypothesis, Y); costVector = vertcat(costVector, cost); #disp("Cost"); #disp(cost); # Step 3 - Updating theta values theta = updateThetaValues(X, Y, theta, hypothesis, learningRate); iterationVector = vertcat(iterationVector, i); endfor function plotGraph(iterationVector, costVector) plot(iterationVector, costVector); ylabel('Cost Function'); xlabel('Iteration'); endfunction plotGraph(iterationVector, costVector); This is the graph I am getting when I am plotting against no.of.iterations and cost function. I am tired by adjusting theta values and learning rate. Can someone help me to solve this problem. Thanks. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I have done a mathematical error. I should have used either power(2.72, -z) or exp(-z). Instead I have used as -1 * power(2.72, z). Now, I'm getting a proper curve. Thanks.

Related questions

0 votes
    I'm trying to implement stochastic gradient descent in MATLAB however I am not seeing any convergence. Mini-batch ... cost of 420. Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    How are the parameters updated during Gradient Descent process? (1)One at a time (2)Simultaneously (3)Sequentially (4)Not updated...
asked May 20, 2021 in Technology by JackTerrance
0 votes
    How is gradient descent (GD) different from Stochastic gradient descent (SGD)?...
asked Mar 11, 2021 in Technology by JackTerrance
0 votes
    How is logistic regression done?...
asked Apr 26, 2021 in Technology by JackTerrance
0 votes
    Does backpropagaion learning is based on gradient descent along error surface? (a) yes (b) no (c) cannot be ... descent but not error surface Please answer the above question....
asked Sep 3, 2022 in Education by JackTerrance
0 votes
    What is gradient descent? (a) method to find the absolute minimum of a function (b) method to find the absolute ... (d) none of the mentioned Please answer the above question....
asked Aug 30, 2022 in Education by JackTerrance
0 votes
    I am having problem in understanding the difference between three optimizers for loss.I went through some documents ... differences? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    ____________ controls the magnitude of a step taken during Gradient Descent. (1)Learning Rate (2)Step Rate (3)Parameter (4)Momentum...
asked May 20, 2021 in Technology by JackTerrance
0 votes
    We use logistic regression to predict the value of the categorical outcome, But I believe linear regression is ... two methodologies. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Which of the following is statistical boosting based on additive logistic regression? (a) gamBoost (b) gbm (c ... and answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    Which of the following is statistical boosting based on additive logistic regression? (a) gamBoost (b) gbm (c) ada (d) mboost...
asked Oct 7, 2021 in Technology by JackTerrance
0 votes
    What are the differences between linear and logistic regression?...
asked Feb 5, 2021 in Technology by JackTerrance
0 votes
    Support Vector Machines, Naive Bayes and Logistic Regression are used for solving ___________________ problems. (a) Clustering (b) Classification (c) Regression (d) Time Series...
asked Oct 19, 2020 in Technology by Editorial Staff
0 votes
    Why Should I Use Numpy Rather Than Idl, Matlab, Octave, Or Yorick?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    Why is NumPy preferred to other programming tools such as Idl, Matlab, Octave, Or Yorick?...
asked Apr 24, 2021 in Technology by JackTerrance
...