Theta computed from Gradient Descent in Linear Regression with one model produces NaN

8 views (last 30 days)
I am running a simple linear regression model with one variable, trying to compute the unit cost of a unit based on the sizes available.The theta value produced from gradient descent function is NaN so I cannot plot the linear regression line. Could someone please help me fix this problem?
data = load('data.txt');
X = data(:,1);
y = data (:, 2);
plotData(X,y);
m = length(X);
X = [ones(m,1), data(:,1)];
theta = zeros(2,1);
J = computeCost(X,y,theta);
fprintf('The cost function J = \n%f \n', J); %need revision
iterations = 1500;
alpha = 0.1;
theta = gradientDescent(X, y, theta, alpha, iterations);
fprintf('Theta computed from gradient descent:\n%f, \n%f', theta(1), theta(2));
  2 Comments
Saurav Shrestha
Saurav Shrestha on 6 Aug 2020
Here is my gradientDescent function
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y);
J_history = zeros(num_iters, 1);
for iter = 1 : num_iters
prediction = X * theta;
error = (prediction - y);
theta = theta - ((alpha/m) * ((error)'*X)');
J_history (iter) = computeCost(X, y, theta);
end
end
Safoura Tanbakouei
Safoura Tanbakouei on 12 Jan 2022
Edited: Safoura Tanbakouei on 12 Jan 2022
@Saurav Shrestha I am doing the same exercise. I do not receive an error but the theta computed from the Gradient Descent is 0 and the plot I get is this figure attached.
This is my code:
temp0 = theta(1,1) - (alpha/m)*sum((X*theta-y));
temp1 = theta(2,1) - (alpha/m)*sum((X*theta-y).*X(:,2));
theta(1,1) = temp0;
theta(2,1) = temp1;

Sign in to comment.

Answers (1)

Raunak Gupta
Raunak Gupta on 14 Aug 2020
Hi,
From the gradientDescent function mentioned in the comments I can see that theta is starting from zero column vector. So, for the very first iteration the prediction vector will also be zero. This makes a big change to the theta value in next iteration. Also, I don’t think the update equation of theta is written such that it will converge. So, I would suggest changing the starting values of theta vector and revisiting the updating equation of theta in gradient descent. I don’t think that computeCost is affecting the theta value.

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!