简体   繁体   中英

Cost function for linear regression with multiple variables in Matlab

The multivariate linear regression cost function:

在此处输入图片说明

Is the following code in Matlab correct?

function J = computeCostMulti(X, y, theta)
    m = length(y);
    J = 0;
    J=(1/(2*m)*(X*theta-y)'*(X*theta-y);
end

Your are missing a ) in the end:

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

There is two ways i tried which is essentially the same code.

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

or you can try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM