Professional Documents
Culture Documents
xDismiss
JointheStackOverflowCommunity
Signup
Can'tunderstandthecostfunctionforLinearRegression
Ireallycan'tunderstandthefollowingequation,especially 1/(2m) .
J(theta_0,theta_1)=1/(2m)*sum_(i=1)^m[h_theta(x^i)y^i]^2
Pleaseexplain.Howitcasts???
editedFeb19at5:10 askedJan13'14at19:05
saurabhagarwal Faheem
649 3 19 62 1 5
3 Thisquestionappearstobeofftopicbecauseitisaboutunderstandingofmathandnottheprogrammingof
math.LanceRoberts Jan13'14at19:07
1 ThisisinMachineLearningbyAndrewNgprofessorinStanford.Fromhislecturesincoursera.Thisis
relatedtocomputerscienceandprogramming.Butifidon'tunderstandit'smathematicsthenhowIcan
developmachineLearningprograms?? Faheem Jan13'14at20:32
Youshoulduseamathsitetounderstandthemath,likethisone: math.stackexchange.com.Butitlooks
likeyouhaveagoodanswernow.LanceRoberts Jan13'14at20:35
1Answer
Thecostfunctionis
J(theta_0,theta_1)=1/(2m)*sum_(i=1)^m[h_theta(x^i)y^i]^2
1/m*sum_(i=1)^m[h_theta(x^i)y^i]^2
This 2 whichappearsinthefrontisusedonlyforsimplificationofthederivative,because
whenyouwilltrytominimizeit,youwillusethesteepestdescentmethod,whichisbasedonthe
derivativeofthisfunction.Derivativeof a^2 is 2a ,andourfunctionisasquareofsomething,so
this 2 willcancelout.Thisistheonlyreasonofitsexistance.
editedMay9at23:06 answeredJan13'14at20:28
lejlot
28.4k 3 24 57
1 Okaysonice.Thisisgreatanswer.Iunderstandnow.Butonequestionagainifudon'tmind.[h_theta(x^i)
y^i]^2issomethinglike(ab)^2whichisequaltoa^2+b^22ab.whywedonotexpand[h_theta(x^i)y^i
]^2like[h_theta(x^i)]^2+y^i]^22[h_theta(x^i)][y^i]?thanks Faheem Jan13'14at20:45
1 Becausethisexpansionwon'tleadtoanysimplification,andonlyaddsadditionaloperations(itischeapier
tocompute(ab)^2thana^22ab+b^2,bacausefirstonerequires2artihmeticoperations,whilethesecond
one6).lejlotJan13'14at20:46
http://stackoverflow.com/questions/21099289/cantunderstandthecostfunctionforlinearregression 1/2
9/23/2016 mathCan'tunderstandthecostfunctionforLinearRegressionStackOverflow
YesbutIthinktheresultsofbothrdifferent.Andthecorrectwayfor(ab)^2isa^22ab+b^2,nottosubtract
bfromafirstthentakesquareoftheresult.MaybeI'mwrong.notprettysure.Sorryforaskingagainand
again. Faheem Jan13'14at20:57
http://stackoverflow.com/questions/21099289/cantunderstandthecostfunctionforlinearregression 2/2