简体   繁体   中英

Are these two cost functions equivalent in octave?

one = sum((X*theta) - y) .^2)/(2*m);
two = ((X'*theta - y)*(X*theta - y) ) / 2*m;

% where X' == X transpose, so I can do matrix multiplication.

"One" is used(successfully) for a uni-variate cost function for linear regression. "One" seems to also work for calculating the cost for a multivariate linear regression problem.

"Two" is the formula suggested for multi-variate approach in a very popular machine learning course :)

Are they the same methods, or did "One" just work per chance on a multi-variate tranning set maybe.

the second formula doesn't look correct to me.

Demo:

>> theta = [2;3];
>> X = [1 2; 3 4; 5 6; 7 8];
>> y = [7;8;9;10];
>> m = length(y);
>> ((X'*theta - y)*(X*theta - y) ) / 2*m
error: operator *: nonconformant arguments (op1 is 2x4, op2 is 2x1)

first formula seems to be correct if we fix a problem with the missing parentheses:

>> sum(((X*theta) - y) .^2)/(2*m)
ans =  155.75

alternative vectorized formula:

>> (X * theta - y)' * (X * theta - y) / (2*m)
ans =  155.75

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