-
-
Save anuvrat/2795554 to your computer and use it in GitHub Desktop.
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 |
For Python users, I found a solution link: https://hongrubb.com/2019/07/25/Coursera%20ML%20Python%20Linear%20Regression%20/
predictions = X*theta; % predictions of hypothesis on examples
sqrErrors = (predictions - y).^2; % squared errors
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
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);
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);
DUDES! but you can't run the file by itself. run the ex1 instead of the computeCost()
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".
J = (1/(2m)) * sum((Xtheta-y).^2)
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
Yes, after running ex1 it is working. Thank you.
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
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.
Guys, you must apply an operation vectorized.
J = [(X* theta - y)' * (X *theta - y)] / (2 * m)
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
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
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
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
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 examplesI 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
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
Guys, you just have to run the ex1.m file.
Otherwise, it will throw an error.
\
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
you have to add the asterixs in the formula otherwise it will throw an error
what is the answer of J as I am getting incorrect answer while submitting
First write your code in computeCost.m don't run this code
then run ex.m and submit the assignment
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
code is worked
thanks a lot bros
@hsmith876
Well done!!!
This work for me
J=(1/(2*m)) * sum((X * theta - y) .^ 2);
Thank you