Skip to content

Instantly share code, notes, and snippets.

@anuvrat
Created May 26, 2012 22:56
Show Gist options
  • Select an option

  • Save anuvrat/2795554 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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

@dleedev365

Copy link
Copy Markdown

@Dhruv261098

Copy link
Copy Markdown

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

@raman89

raman89 commented Apr 26, 2020

Copy link
Copy Markdown

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

MPdoor commented May 19, 2020

Copy link
Copy Markdown

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

MPdoor commented May 19, 2020

Copy link
Copy Markdown

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

MPdoor commented May 19, 2020

Copy link
Copy Markdown

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

@MPdoor

MPdoor commented May 19, 2020

Copy link
Copy Markdown

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
Copy Markdown

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

srudrabhatla commented Jul 18, 2020

Copy link
Copy Markdown

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

@KLPranav

Copy link
Copy Markdown

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
Copy Markdown

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

@maxnze1

maxnze1 commented Oct 26, 2020

Copy link
Copy Markdown

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

KLPranav commented Oct 29, 2020 via email

Copy link
Copy Markdown

@omgitsmj24

Copy link
Copy Markdown

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
Copy Markdown

Guys, you must apply an operation vectorized.

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

@MariaMasood-1

Copy link
Copy Markdown

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
Copy Markdown

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

ramy21 commented Apr 8, 2021

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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

ghost commented Jun 5, 2021

Copy link
Copy Markdown

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

@jackinhub

jackinhub commented Jun 24, 2021

Copy link
Copy Markdown

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
Copy Markdown

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

@rohan5901

Copy link
Copy Markdown

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

ghost commented Aug 3, 2021

Copy link
Copy Markdown

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

@AswinAzikar

Copy link
Copy Markdown

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
Copy Markdown

code is worked

@mojito-1

Copy link
Copy Markdown

thanks a lot bros

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