How to compute Cost function for linear regression

조회 수: 39 (최근 30일)
Muhammad Kundi
Muhammad Kundi 2019년 6월 22일
댓글: Abdulwaliyi 2022년 12월 12일
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
Muhammad Kundi
Muhammad Kundi 2019년 6월 23일
sorry...I forgot to mention...I just hit run to execute the program and I get this error message..
Do I have to run the function by inserting the values of x,y and theta???
David Onoja
David Onoja 2022년 2월 4일
Pls how do you submit answers

댓글을 달려면 로그인하십시오.

답변 (10개)

João Marlon Souto Ferraz
João Marlon Souto Ferraz 2020년 9월 14일
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
Mahdi Badriyan 2020년 5월 25일
hi
J = sum(sError) / (2 .* m);
in this way your code will be corrected.

Sanskar Choudhary
Sanskar Choudhary 2021년 12월 28일
open file submit.m and then run it.(don't run computeCost.m file, this will not work because its just function)
  댓글 수: 3
David Onoja
David Onoja 2022년 2월 4일
Pls explain better
Abdulwaliyi
Abdulwaliyi 2022년 12월 12일
this is a nice answer
just use submit() then generate your token and run your answer

댓글을 달려면 로그인하십시오.


Ghayyur Abbas
Ghayyur Abbas 2019년 8월 29일
initialize x y theta with values in the function

Heng Dao
Heng Dao 2021년 2월 6일
((h)-y).^2 Try try hope can help u

Prasad Kute
Prasad Kute 2021년 7월 23일
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
amenallah  berrejeb
amenallah berrejeb 2022년 2월 6일
it doesn't work
Image Analyst
Image Analyst 2022년 2월 6일
@amenallah berrejeb Why not? Show your code that proves it does not work.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2022년 2월 4일
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
Image Analyst 2022년 2월 6일
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
Jia He 2022년 2월 6일
If you meet errors in submitting, and your function is correct. Try to delete its .mlx file and submit it again.

MAZEN ALHARBI
MAZEN ALHARBI 2022년 4월 5일
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
safwan bin amir
safwan bin amir 2022년 4월 10일
@MAZEN ALHARBI you a life saviour. you are doing that ML course of andrew ng?

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by