How to compute Cost function for linear regression
Mostrar comentarios más antiguos
Hi,
I am trying to compute cost function

I am using the following code:
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
% 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.
h = X * theta;
sError = (h - y) .^ 2;
J = sum(sError) / (2 * m);
% =========================================================================
end
.But I am getting the following error
Error in computeCost (line 7)
m = length(y); % number of training examples
.Please help
6 comentarios
infinity
el 22 de Jun. de 2019
Hello,
in your code, what is "thetha" in
diff=thetha'*X - y;
Muhammad Kundi
el 22 de Jun. de 2019
Editada: Muhammad Kundi
el 22 de Jun. de 2019
infinity
el 22 de Jun. de 2019
The error tell us that
Error in computeCostMulti (line 7)
but you show the function name
computeCost
they are not fit. Do you have the function "computeCostMulti"
Muhammad Kundi
el 23 de Jun. de 2019
Muhammad Kundi
el 23 de Jun. de 2019
David Onoja
el 4 de Feb. de 2022
Pls how do you submit answers
Respuestas (10)
João Marlon Souto Ferraz
el 14 de Sept. de 2020
4 votos
I believe than this is happen because you are running and trying to compile the function. Just save and to call than function from the other script.
Mahdi Badriyan
el 25 de Mayo de 2020
hi
J = sum(sError) / (2 .* m);
in this way your code will be corrected.
1 comentario
Arunodhaya
el 10 de Oct. de 2022
Hello
Sanskar Choudhary
el 28 de Dic. de 2021
1 voto
open file submit.m and then run it.(don't run computeCost.m file, this will not work because its just function)
3 comentarios
Chinedu Adiegwu
el 18 de En. de 2022
I don't understand, could you rephrase?
David Onoja
el 4 de Feb. de 2022
Pls explain better
Abdulwaliyi
el 12 de Dic. de 2022
this is a nice answer
just use submit() then generate your token and run your answer
Ghayyur Abbas
el 29 de Ag. de 2019
0 votos
initialize x y theta with values in the function
2 comentarios
Nagesh Rathod
el 16 de Mayo de 2020
how
Tushant Verma
el 6 de Ag. de 2020
computeCost(X,y,theta)
Heng Dao
el 6 de Feb. de 2021
0 votos
((h)-y).^2 Try try hope can help u
Prasad Kute
el 23 de Jul. de 2021
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
% 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 = (1/(2*m))*sum(((X*theta) - y).^2)
Try this, you will get it correct.
2 comentarios
amenallah berrejeb
el 6 de Feb. de 2022
it doesn't work
Image Analyst
el 6 de Feb. de 2022
Image Analyst
el 4 de Feb. de 2022
You say "sorry...I forgot to mention...I just hit run to execute the program and I get this error message.."
Well that is the whole problem. When you have a function that expects inputs like X, y, theta then you need to supply those inputs. You can't just click the green run triangle and expect somehow that doing that will automagically invent values for X, y, and theta. So you need to do something like
% Declare inputs:
X = rand(1,10);
y = rand(1, 10);
theta = 42;
% Now run the function:
result = computeCost(X, y, theta)
fprintf('Done with the program!\n');
%==========================================================================================
% Define the function:
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
% 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.
h = X * theta;
sError = (h - y) .^ 2;
J = sum(sError) / (2 * m);
end
Image Analyst
el 6 de Feb. de 2022
As long as y is defined (like you assigned something to y before you called the function) then that line
m = length(y); % number of training examples
should work. However you didn't give the entire error message. You gave the line the error occurred on but not the actual error description. Why not? You need to include all the red text, not just some of it.
By the way you need to assign something for y, you can't just click the green Run triangle on the toolbar without assigning all the input variables.
Jia He
el 6 de Feb. de 2022
0 votos
If you meet errors in submitting, and your function is correct. Try to delete its .mlx file and submit it again.
MAZEN ALHARBI
el 5 de Abr. de 2022
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
1 comentario
safwan bin amir
el 10 de Abr. de 2022
@MAZEN ALHARBI you a life saviour. you are doing that ML course of andrew ng?
Categorías
Más información sobre Linear and Nonlinear Regression en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!