Skip to content

Instantly share code, notes, and snippets.

@anuvrat
Created May 26, 2012 22:56
Show Gist options
  • Save anuvrat/2795554 to your computer and use it in GitHub Desktop.
Save anuvrat/2795554 to your computer and use it in GitHub Desktop.
Compute cost for linear regression
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% We need to return the following variable
J = sum((X * theta - y) .^ 2) / (2*m)
end
@AhmedEhabH
Copy link

@hsmith876
Well done!!!
This work for me
J=(1/(2*m)) * sum((X * theta - y) .^ 2);
Thank you

@dleedev365
Copy link

@Dhruv261098
Copy link

predictions = X*theta; % predictions of hypothesis on examples
sqrErrors = (predictions - y).^2; % squared errors

@raman89
Copy link

raman89 commented Apr 26, 2020

J=(1/(2*m)) * sum((X * theta - y) .^ 2);

yes its working.As i have find we don't need to run compute cost only we need to run ex1 and then need to submit.Then its submitted.Thanks

@MPdoor
Copy link

MPdoor commented May 19, 2020

I SOLVED THE ISSUE OF LINE 7! The thing is, is that in that particular document, the variables theta X and y are not defined, and you didn't load the data into the document. So you need to add all the previous information into the document.

so put this into your "initialize some useful values"

data = load('ex1data1.txt'); % read comma separated data
y = data(:, 2);
m = length(y); % number of training examples
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1);

@MPdoor
Copy link

MPdoor commented May 19, 2020

HI, on running this code, it throws an error :

computeCost
Not enough input arguments.

Error in computeCost (line 7)
m = length(y); % number of training examples

I SOLVED THE ISSUE OF LINE 7! The thing is, is that in that particular document, the variables theta X and y are not defined, and you didn't load the data into the document. So you need to add all the previous information into the document.

so put this into your "initialize some useful values"

data = load('ex1data1.txt'); % read comma separated data
y = data(:, 2);
m = length(y); % number of training examples
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1);

@MPdoor
Copy link

MPdoor commented May 19, 2020

DUDES! but you can't run the file by itself. run the ex1 instead of the computeCost()

@MPdoor
Copy link

MPdoor commented May 19, 2020

These are functions, not command scripts. You can't "run" a function unless you also provide it the required function parameters. Doing so will give error messages about "not enough arguments" or "undefined variables".

@tamalmallick
Copy link

After running ex1.m when I run computeCost the problem exists. How shall I run ex1.m to solve this issue?
Can you please elaborate @MPdoor and @kulvirk,?

@srudrabhatla
Copy link

srudrabhatla commented Jul 18, 2020

J = (1/(2m)) * sum((Xtheta-y).^2)

@KLPranav
Copy link

J=(1/(2*m)) * sum((X * theta - y) .^ 2);

yes its working.As i have find we don't need to run compute cost only we need to run ex1 and then need to submit.Then its submitted.Thanks

@KLPranav
Copy link

Yes, after running ex1 it is working. Thank you.

@maxnze1
Copy link

maxnze1 commented Oct 26, 2020

Am facing some challenges submitting, the error message am getting is invalid call to script (the path to where i saved the folder). but i was able to submit the first one which is matrix.
Kindly help

@KLPranav
Copy link

KLPranav commented Oct 29, 2020 via email

@omgitsmj24
Copy link

What you all get wrong is you can't run computeCost, GradientDescent! You can only run those function if it has parameter in them as shown in the code. For computeCost, run computeCost(X, y, theta) or computeCost(X, y,[-1; 2]) to check your results which already has been written in the ex1.mlx instruction.

@franciscofwo
Copy link

Guys, you must apply an operation vectorized.

J = [(X* theta - y)' * (X *theta - y)] / (2 * m)

@MariaMasood-1
Copy link

HI, on running this code, it throws an error :

computeCost
Not enough input arguments.

Error in computeCost (line 7)
m = length(y); % number of training examples

You should add this line:
data = load('ex1data1.txt'); % read comma separated data
X = data(:, 1); y = data(:, 2);
to avoid the error

@najvajam
Copy link

this worked for me:
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y

% Initialize some useful values
% number of training examples

% You need to return the following variables correctly
data = load('ex1data1.txt'); % read comma separated data
y = data(:, 2);
m = length(y); % number of training examples
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1);
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
J = 0;
J = 1/(2*m) * (X * theta - y)' * (X * theta - y); % vectorized form

% =========================================================================
end

@ramy21
Copy link

ramy21 commented Apr 8, 2021

X = [ones(m, 1), data(:,1)];
this line gives me an error of
File "", line 37
X = [ones(m, 1), data(:,1)];
^
SyntaxError: invalid syntax

@cadgeroptical
Copy link

DUDES! but you can't run the file by itself. run the ex1 instead of the computeCost()

I SOLVED THE ISSUE OF LINE 7! The thing is, is that in that particular document, the variables theta X and y are not defined, and you didn't load the data into the document. So you need to add all the previous information into the document.

so put this into your "initialize some useful values"

data = load('ex1data1.txt'); % read comma separated data
y = data(:, 2);
m = length(y); % number of training examples
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1);

THANK YOU SO MUCH! :D

@anhtruong1209
Copy link

HI, on running this code, it throws an error :

computeCost
Not enough input arguments.

Error in computeCost (line 7)
m = length(y); % number of training examples

I SOLVED THE ISSUE OF LINE 7! The thing is, is that in that particular document, the variables theta X and y are not defined, and you didn't load the data into the document. So you need to add all the previous information into the document.

so put this into your "initialize some useful values"

data = load('ex1data1.txt'); % read comma separated data
y = data(:, 2);
m = length(y); % number of training examples
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1);

thanks you so much

@sourabmaity
Copy link

HI, on running this code, it throws an error :

computeCost
Not enough input arguments.

Error in computeCost (line 7)
m = length(y); % number of training examples

You have to run ex1.m not computrCost.m

Copy link

ghost commented Jun 5, 2021

Guys, you just have to run the ex1.m file.
Otherwise, it will throw an error.
\

@jackinhub
Copy link

jackinhub commented Jun 24, 2021

Hi Guys,
Here is my solution.

function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
data = load('ex1data1.txt'); % read comma separated data

% Initialize some useful values
m = length(data); % number of training examples
X = [ones(m,1),data(:,1)]; y = data(:, 2);
theta = zeros(2,1);
% You need to return the following variables correctly
%J = 0;

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.

J = sum((Xtheta - y).^2)/(2m);

% =========================================================================

end

@Abhishek-michael
Copy link

you have to add the asterixs in the formula otherwise it will throw an error

@rohan5901
Copy link

what is the answer of J as I am getting incorrect answer while submitting

Copy link

ghost commented Aug 3, 2021

First write your code in computeCost.m don't run this code
then run ex.m and submit the assignment

@AswinAzikar
Copy link

data = load('ex1data1.txt'); % read comma separated data
y = data(:, 2);
m = length(y); % number of training examples
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1);
excellent

@aravindhanchandran
Copy link

code is worked

@mojito-1
Copy link

thanks a lot bros

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment