[{"data":1,"prerenderedAt":3440},["ShallowReactive",2],{"lang-switch-post-\u002Fen\u002Fplaylists\u002Fmachine-learning-specialization\u002Flab03-cost-function":3,"post-en-machine-learning-specialization-lab03-cost-function":4},"\u002Fplaylists\u002Fmachine-learning-specialization\u002Flab03-cost-function",{"id":5,"title":6,"body":7,"cover":3425,"date":3426,"description":3427,"extension":3428,"meta":3429,"navigation":1843,"order":1828,"path":3430,"playlist":3431,"seo":3432,"status":3433,"stem":3434,"tags":3435,"__hash__":3439},"posts\u002Fen\u002Fplaylists\u002Fmachine-learning-specialization\u002Flab03-cost-function.md","Optional Lab: the Cost Function",{"type":8,"value":9,"toc":3415},"minimark",[10,153,231,777,780,785,793,797,1027,1624,1627,1772,1805,1809,1965,1980,1984,2123,2171,2182,2185,2427,2482,2486,2489,2550,2606,2669,2676,2790,2802,2806,2814,2989,2994,3001,3005,3294,3304,3308,3324,3388,3403,3406,3411],[11,12,13,18,19,97,98,152],"p",{},[14,15,17],"a",{"href":16},"\u002Fen\u002Fplaylists\u002Fmachine-learning-specialization\u002Flab02-model-representation","In the last post"," you found ",[20,21,24,55],"span",{"className":22},[23],"katex",[20,25,28],{"className":26},[27],"katex-mathml",[29,30,32],"math",{"xmlns":31},"http:\u002F\u002Fwww.w3.org\u002F1998\u002FMath\u002FMathML",[33,34,35,50],"semantics",{},[36,37,38,42,46],"mrow",{},[39,40,41],"mi",{},"w",[43,44,45],"mo",{},"=",[47,48,49],"mn",{},"200",[51,52,54],"annotation",{"encoding":53},"application\u002Fx-tex","w = 200",[20,56,60,87],{"className":57,"ariaHidden":59},[58],"katex-html","true",[20,61,64,69,75,80,84],{"className":62},[63],"base",[20,65],{"className":66,"style":68},[67],"strut","height:0.4306em;",[20,70,41],{"className":71,"style":74},[72,73],"mord","mathnormal","margin-right:0.0269em;",[20,76],{"className":77,"style":79},[78],"mspace","margin-right:0.2778em;",[20,81,45],{"className":82},[83],"mrel",[20,85],{"className":86,"style":79},[78],[20,88,90,94],{"className":89},[63],[20,91],{"className":92,"style":93},[67],"height:0.6444em;",[20,95,49],{"className":96},[72]," and ",[20,99,101,121],{"className":100},[23],[20,102,104],{"className":103},[27],[29,105,106],{"xmlns":31},[33,107,108,118],{},[36,109,110,113,115],{},[39,111,112],{},"b",[43,114,45],{},[47,116,117],{},"100",[51,119,120],{"encoding":53},"b = 100",[20,122,124,143],{"className":123,"ariaHidden":59},[58],[20,125,127,131,134,137,140],{"className":126},[63],[20,128],{"className":129,"style":130},[67],"height:0.6944em;",[20,132,112],{"className":133},[72,73],[20,135],{"className":136,"style":79},[78],[20,138,45],{"className":139},[83],[20,141],{"className":142,"style":79},[78],[20,144,146,149],{"className":145},[63],[20,147],{"className":148,"style":93},[67],[20,150,117],{"className":151},[72]," by dragging a slider until the error hit zero. Cool, it worked. But wait. How did you know you'd found the \"best\" fit? You watched the total error number drop to zero and trusted it.",[11,154,155,156,161,162,230],{},"This post is about giving that number a name, a last name, and a formula. It's called the ",[157,158,160],"glossary-term",{"definition":159},"a formula that summarizes, in a single number, how wrong a model is on the training data","cost function",", written ",[20,163,165,194],{"className":164},[23],[20,166,168],{"className":167},[27],[29,169,170],{"xmlns":31},[33,171,172,191],{},[36,173,174,177,181,183,186,188],{},[39,175,176],{},"J",[43,178,180],{"stretchy":179},"false","(",[39,182,41],{},[43,184,185],{"separator":59},",",[39,187,112],{},[43,189,190],{"stretchy":179},")",[51,192,193],{"encoding":53},"J(w,b)",[20,195,197],{"className":196,"ariaHidden":59},[58],[20,198,200,204,208,212,215,219,223,226],{"className":199},[63],[20,201],{"className":202,"style":203},[67],"height:1em;vertical-align:-0.25em;",[20,205,176],{"className":206,"style":207},[72,73],"margin-right:0.0962em;",[20,209,180],{"className":210},[211],"mopen",[20,213,41],{"className":214,"style":74},[72,73],[20,216,185],{"className":217},[218],"mpunct",[20,220],{"className":221,"style":222},[78],"margin-right:0.1667em;",[20,224,112],{"className":225},[72,73],[20,227,190],{"className":228},[229],"mclose",".",[11,232,233],{},[20,234,236,358],{"className":235},[23],[20,237,239],{"className":238},[27],[29,240,241],{"xmlns":31},[33,242,243,355],{},[36,244,245,247,249,251,253,255,257,259,273,298],{},[39,246,176],{},[43,248,180],{"stretchy":179},[39,250,41],{},[43,252,185],{"separator":59},[39,254,112],{},[43,256,190],{"stretchy":179},[43,258,45],{},[260,261,262,265],"mfrac",{},[47,263,264],{},"1",[36,266,267,270],{},[47,268,269],{},"2",[39,271,272],{},"m",[274,275,276,279,289],"msubsup",{},[43,277,278],{},"∑",[36,280,281,284,286],{},[39,282,283],{},"i",[43,285,45],{},[47,287,288],{},"0",[36,290,291,293,296],{},[39,292,272],{},[43,294,295],{},"−",[47,297,264],{},[299,300,301,353],"msup",{},[36,302,303,305,319,321,334,336,338,351],{},[43,304,180],{"fence":59},[306,307,308,311],"msub",{},[39,309,310],{},"f",[36,312,313,315,317],{},[39,314,41],{},[43,316,185],{"separator":59},[39,318,112],{},[43,320,180],{"stretchy":179},[299,322,323,326],{},[39,324,325],{},"x",[36,327,328,330,332],{},[43,329,180],{"stretchy":179},[39,331,283],{},[43,333,190],{"stretchy":179},[43,335,190],{"stretchy":179},[43,337,295],{},[299,339,340,343],{},[39,341,342],{},"y",[36,344,345,347,349],{},[43,346,180],{"stretchy":179},[39,348,283],{},[43,350,190],{"stretchy":179},[43,352,190],{"fence":59},[47,354,269],{},[51,356,357],{"encoding":53},"J(w,b) = \\frac{1}{2m}\\sum_{i=0}^{m-1}\\left(f_{w,b}(x^{(i)}) - y^{(i)}\\right)^2",[20,359,361,397],{"className":360,"ariaHidden":59},[58],[20,362,364,367,370,373,376,379,382,385,388,391,394],{"className":363},[63],[20,365],{"className":366,"style":203},[67],[20,368,176],{"className":369,"style":207},[72,73],[20,371,180],{"className":372},[211],[20,374,41],{"className":375,"style":74},[72,73],[20,377,185],{"className":378},[218],[20,380],{"className":381,"style":222},[78],[20,383,112],{"className":384},[72,73],[20,386,190],{"className":387},[229],[20,389],{"className":390,"style":79},[78],[20,392,45],{"className":393},[83],[20,395],{"className":396,"style":79},[78],[20,398,400,404,495,498,578,581],{"className":399},[63],[20,401],{"className":402,"style":403},[67],"height:1.442em;vertical-align:-0.35em;",[20,405,407,411,492],{"className":406},[72],[20,408],{"className":409},[211,410],"nulldelimiter",[20,412,414],{"className":413},[260],[20,415,419,483],{"className":416},[417,418],"vlist-t","vlist-t2",[20,420,423,478],{"className":421},[422],"vlist-r",[20,424,428,452,463],{"className":425,"style":427},[426],"vlist","height:0.8451em;",[20,429,431,436],{"style":430},"top:-2.655em;",[20,432],{"className":433,"style":435},[434],"pstrut","height:3em;",[20,437,443],{"className":438},[439,440,441,442],"sizing","reset-size6","size3","mtight",[20,444,446,449],{"className":445},[72,442],[20,447,269],{"className":448},[72,442],[20,450,272],{"className":451},[72,73,442],[20,453,455,458],{"style":454},"top:-3.23em;",[20,456],{"className":457,"style":435},[434],[20,459],{"className":460,"style":462},[461],"frac-line","border-bottom-width:0.04em;",[20,464,466,469],{"style":465},"top:-3.394em;",[20,467],{"className":468,"style":435},[434],[20,470,472],{"className":471},[439,440,441,442],[20,473,475],{"className":474},[72,442],[20,476,264],{"className":477},[72,442],[20,479,482],{"className":480},[481],"vlist-s","​",[20,484,486],{"className":485},[422],[20,487,490],{"className":488,"style":489},[426],"height:0.345em;",[20,491],{},[20,493],{"className":494},[229,410],[20,496],{"className":497,"style":222},[78],[20,499,502,508],{"className":500},[501],"mop",[20,503,278],{"className":504,"style":507},[501,505,506],"op-symbol","small-op","position:relative;top:0em;",[20,509,512],{"className":510},[511],"msupsub",[20,513,515,569],{"className":514},[417,418],[20,516,518,566],{"className":517},[422],[20,519,522,544],{"className":520,"style":521},[426],"height:0.954em;",[20,523,525,529],{"style":524},"top:-2.4003em;margin-left:0em;margin-right:0.05em;",[20,526],{"className":527,"style":528},[434],"height:2.7em;",[20,530,532],{"className":531},[439,440,441,442],[20,533,535,538,541],{"className":534},[72,442],[20,536,283],{"className":537},[72,73,442],[20,539,45],{"className":540},[83,442],[20,542,288],{"className":543},[72,442],[20,545,547,550],{"style":546},"top:-3.2029em;margin-right:0.05em;",[20,548],{"className":549,"style":528},[434],[20,551,553],{"className":552},[439,440,441,442],[20,554,556,559,563],{"className":555},[72,442],[20,557,272],{"className":558},[72,73,442],[20,560,295],{"className":561},[562,442],"mbin",[20,564,264],{"className":565},[72,442],[20,567,482],{"className":568},[481],[20,570,572],{"className":571},[422],[20,573,576],{"className":574,"style":575},[426],"height:0.2997em;",[20,577],{},[20,579],{"className":580,"style":222},[78],[20,582,585,752],{"className":583},[584],"minner",[20,586,588,598,651,654,694,697,701,704,707,746],{"className":587},[584],[20,589,593],{"className":590,"style":592},[211,591],"delimcenter","top:0em;",[20,594,180],{"className":595},[596,597],"delimsizing","size1",[20,599,601,605],{"className":600},[72],[20,602,310],{"className":603,"style":604},[72,73],"margin-right:0.1076em;",[20,606,608],{"className":607},[511],[20,609,611,642],{"className":610},[417,418],[20,612,614,639],{"className":613},[422],[20,615,618],{"className":616,"style":617},[426],"height:0.3361em;",[20,619,621,624],{"style":620},"top:-2.55em;margin-left:-0.1076em;margin-right:0.05em;",[20,622],{"className":623,"style":528},[434],[20,625,627],{"className":626},[439,440,441,442],[20,628,630,633,636],{"className":629},[72,442],[20,631,41],{"className":632,"style":74},[72,73,442],[20,634,185],{"className":635},[218,442],[20,637,112],{"className":638},[72,73,442],[20,640,482],{"className":641},[481],[20,643,645],{"className":644},[422],[20,646,649],{"className":647,"style":648},[426],"height:0.2861em;",[20,650],{},[20,652,180],{"className":653},[211],[20,655,657,660],{"className":656},[72],[20,658,325],{"className":659},[72,73],[20,661,663],{"className":662},[511],[20,664,666],{"className":665},[417],[20,667,669],{"className":668},[422],[20,670,673],{"className":671,"style":672},[426],"height:0.888em;",[20,674,676,679],{"style":675},"top:-3.063em;margin-right:0.05em;",[20,677],{"className":678,"style":528},[434],[20,680,682],{"className":681},[439,440,441,442],[20,683,685,688,691],{"className":684},[72,442],[20,686,180],{"className":687},[211,442],[20,689,283],{"className":690},[72,73,442],[20,692,190],{"className":693},[229,442],[20,695,190],{"className":696},[229],[20,698],{"className":699,"style":700},[78],"margin-right:0.2222em;",[20,702,295],{"className":703},[562],[20,705],{"className":706,"style":700},[78],[20,708,710,714],{"className":709},[72],[20,711,342],{"className":712,"style":713},[72,73],"margin-right:0.0359em;",[20,715,717],{"className":716},[511],[20,718,720],{"className":719},[417],[20,721,723],{"className":722},[422],[20,724,726],{"className":725,"style":672},[426],[20,727,728,731],{"style":675},[20,729],{"className":730,"style":528},[434],[20,732,734],{"className":733},[439,440,441,442],[20,735,737,740,743],{"className":736},[72,442],[20,738,180],{"className":739},[211,442],[20,741,283],{"className":742},[72,73,442],[20,744,190],{"className":745},[229,442],[20,747,749],{"className":748,"style":592},[229,591],[20,750,190],{"className":751},[596,597],[20,753,755],{"className":754},[511],[20,756,758],{"className":757},[417],[20,759,761],{"className":760},[422],[20,762,765],{"className":763,"style":764},[426],"height:1.092em;",[20,766,768,771],{"style":767},"top:-3.3409em;margin-right:0.05em;",[20,769],{"className":770,"style":528},[434],[20,772,774],{"className":773},[439,440,441,442],[20,775,269],{"className":776},[72,442],[11,778,779],{},"I know, it looks scary. Let's take that formula apart piece by piece, like disassembling a Lego set.",[781,782,784],"h2",{"id":783},"watch-out-for-the-word-cost","Watch out for the word \"cost\"",[11,786,787,788,792],{},"Before anything else: here ",[789,790,791],"em",{},"cost"," has nothing to do with a house's price. It's the measure of how wrong the model is. For the house's value we keep using the word price. This vocabulary mix-up trips a lot of people up early on, because \"cost\" in everyday speech always means money.",[781,794,796],{"id":795},"taking-the-formula-apart","Taking the formula apart",[11,798,799,800,875,876,1022,1023,230],{},"Think of a target-shooting contest. Each house in your training set is an attempt to hit the bullseye (the real price, ",[20,801,803,827],{"className":802},[23],[20,804,806],{"className":805},[27],[29,807,808],{"xmlns":31},[33,809,810,824],{},[36,811,812],{},[299,813,814,816],{},[39,815,342],{},[36,817,818,820,822],{},[43,819,180],{"stretchy":179},[39,821,283],{},[43,823,190],{"stretchy":179},[51,825,826],{"encoding":53},"y^{(i)}",[20,828,830],{"className":829,"ariaHidden":59},[58],[20,831,833,837],{"className":832},[63],[20,834],{"className":835,"style":836},[67],"height:1.0824em;vertical-align:-0.1944em;",[20,838,840,843],{"className":839},[72],[20,841,342],{"className":842,"style":713},[72,73],[20,844,846],{"className":845},[511],[20,847,849],{"className":848},[417],[20,850,852],{"className":851},[422],[20,853,855],{"className":854,"style":672},[426],[20,856,857,860],{"style":675},[20,858],{"className":859,"style":528},[434],[20,861,863],{"className":862},[439,440,441,442],[20,864,866,869,872],{"className":865},[72,442],[20,867,180],{"className":868},[211,442],[20,870,283],{"className":871},[72,73,442],[20,873,190],{"className":874},[229,442],"). ",[20,877,879,919],{"className":878},[23],[20,880,882],{"className":881},[27],[29,883,884],{"xmlns":31},[33,885,886,916],{},[36,887,888,900,902,914],{},[306,889,890,892],{},[39,891,310],{},[36,893,894,896,898],{},[39,895,41],{},[43,897,185],{"separator":59},[39,899,112],{},[43,901,180],{"stretchy":179},[299,903,904,906],{},[39,905,325],{},[36,907,908,910,912],{},[43,909,180],{"stretchy":179},[39,911,283],{},[43,913,190],{"stretchy":179},[43,915,190],{"stretchy":179},[51,917,918],{"encoding":53},"f_{w,b}(x^{(i)})",[20,920,922],{"className":921,"ariaHidden":59},[58],[20,923,925,929,978,981,1019],{"className":924},[63],[20,926],{"className":927,"style":928},[67],"height:1.1741em;vertical-align:-0.2861em;",[20,930,932,935],{"className":931},[72],[20,933,310],{"className":934,"style":604},[72,73],[20,936,938],{"className":937},[511],[20,939,941,970],{"className":940},[417,418],[20,942,944,967],{"className":943},[422],[20,945,947],{"className":946,"style":617},[426],[20,948,949,952],{"style":620},[20,950],{"className":951,"style":528},[434],[20,953,955],{"className":954},[439,440,441,442],[20,956,958,961,964],{"className":957},[72,442],[20,959,41],{"className":960,"style":74},[72,73,442],[20,962,185],{"className":963},[218,442],[20,965,112],{"className":966},[72,73,442],[20,968,482],{"className":969},[481],[20,971,973],{"className":972},[422],[20,974,976],{"className":975,"style":648},[426],[20,977],{},[20,979,180],{"className":980},[211],[20,982,984,987],{"className":983},[72],[20,985,325],{"className":986},[72,73],[20,988,990],{"className":989},[511],[20,991,993],{"className":992},[417],[20,994,996],{"className":995},[422],[20,997,999],{"className":998,"style":672},[426],[20,1000,1001,1004],{"style":675},[20,1002],{"className":1003,"style":528},[434],[20,1005,1007],{"className":1006},[439,440,441,442],[20,1008,1010,1013,1016],{"className":1009},[72,442],[20,1011,180],{"className":1012},[211,442],[20,1014,283],{"className":1015},[72,73,442],[20,1017,190],{"className":1018},[229,442],[20,1020,190],{"className":1021},[229]," is where your arrow actually landed (the model's prediction). The distance between where it landed and the bullseye is that shot's ",[157,1024,1026],{"definition":1025},"the difference between what the model predicted and the real value for that example, also called a residual","error",[1028,1029,1030,1047],"table",{},[1031,1032,1033],"thead",{},[1034,1035,1036,1041,1044],"tr",{},[1037,1038,1040],"th",{"align":1039},"left","Piece of the formula",[1037,1042,1043],{"align":1039},"Name",[1037,1045,1046],{"align":1039},"What it does",[1048,1049,1050,1273,1355,1479],"tbody",{},[1034,1051,1052,1267,1270],{},[1053,1054,1055],"td",{"align":1039},[20,1056,1058,1112],{"className":1057},[23],[20,1059,1061],{"className":1060},[27],[29,1062,1063],{"xmlns":31},[33,1064,1065,1109],{},[36,1066,1067,1079,1081,1093,1095,1097],{},[306,1068,1069,1071],{},[39,1070,310],{},[36,1072,1073,1075,1077],{},[39,1074,41],{},[43,1076,185],{"separator":59},[39,1078,112],{},[43,1080,180],{"stretchy":179},[299,1082,1083,1085],{},[39,1084,325],{},[36,1086,1087,1089,1091],{},[43,1088,180],{"stretchy":179},[39,1090,283],{},[43,1092,190],{"stretchy":179},[43,1094,190],{"stretchy":179},[43,1096,295],{},[299,1098,1099,1101],{},[39,1100,342],{},[36,1102,1103,1105,1107],{},[43,1104,180],{"stretchy":179},[39,1106,283],{},[43,1108,190],{"stretchy":179},[51,1110,1111],{"encoding":53},"f_{w,b}(x^{(i)}) - y^{(i)}",[20,1113,1115,1223],{"className":1114,"ariaHidden":59},[58],[20,1116,1118,1121,1170,1173,1211,1214,1217,1220],{"className":1117},[63],[20,1119],{"className":1120,"style":928},[67],[20,1122,1124,1127],{"className":1123},[72],[20,1125,310],{"className":1126,"style":604},[72,73],[20,1128,1130],{"className":1129},[511],[20,1131,1133,1162],{"className":1132},[417,418],[20,1134,1136,1159],{"className":1135},[422],[20,1137,1139],{"className":1138,"style":617},[426],[20,1140,1141,1144],{"style":620},[20,1142],{"className":1143,"style":528},[434],[20,1145,1147],{"className":1146},[439,440,441,442],[20,1148,1150,1153,1156],{"className":1149},[72,442],[20,1151,41],{"className":1152,"style":74},[72,73,442],[20,1154,185],{"className":1155},[218,442],[20,1157,112],{"className":1158},[72,73,442],[20,1160,482],{"className":1161},[481],[20,1163,1165],{"className":1164},[422],[20,1166,1168],{"className":1167,"style":648},[426],[20,1169],{},[20,1171,180],{"className":1172},[211],[20,1174,1176,1179],{"className":1175},[72],[20,1177,325],{"className":1178},[72,73],[20,1180,1182],{"className":1181},[511],[20,1183,1185],{"className":1184},[417],[20,1186,1188],{"className":1187},[422],[20,1189,1191],{"className":1190,"style":672},[426],[20,1192,1193,1196],{"style":675},[20,1194],{"className":1195,"style":528},[434],[20,1197,1199],{"className":1198},[439,440,441,442],[20,1200,1202,1205,1208],{"className":1201},[72,442],[20,1203,180],{"className":1204},[211,442],[20,1206,283],{"className":1207},[72,73,442],[20,1209,190],{"className":1210},[229,442],[20,1212,190],{"className":1213},[229],[20,1215],{"className":1216,"style":700},[78],[20,1218,295],{"className":1219},[562],[20,1221],{"className":1222,"style":700},[78],[20,1224,1226,1229],{"className":1225},[63],[20,1227],{"className":1228,"style":836},[67],[20,1230,1232,1235],{"className":1231},[72],[20,1233,342],{"className":1234,"style":713},[72,73],[20,1236,1238],{"className":1237},[511],[20,1239,1241],{"className":1240},[417],[20,1242,1244],{"className":1243},[422],[20,1245,1247],{"className":1246,"style":672},[426],[20,1248,1249,1252],{"style":675},[20,1250],{"className":1251,"style":528},[434],[20,1253,1255],{"className":1254},[439,440,441,442],[20,1256,1258,1261,1264],{"className":1257},[72,442],[20,1259,180],{"className":1260},[211,442],[20,1262,283],{"className":1263},[72,73,442],[20,1265,190],{"className":1266},[229,442],[1053,1268,1269],{"align":1039},"error (residual)",[1053,1271,1272],{"align":1039},"the distance between the prediction and the real value",[1034,1274,1275,1349,1352],{},[1053,1276,1277],{"align":1039},[20,1278,1280,1303],{"className":1279},[23],[20,1281,1283],{"className":1282},[27],[29,1284,1285],{"xmlns":31},[33,1286,1287,1300],{},[36,1288,1289,1291,1294],{},[43,1290,180],{"stretchy":179},[43,1292,1293],{},"…",[299,1295,1296,1298],{},[43,1297,190],{"stretchy":179},[47,1299,269],{},[51,1301,1302],{"encoding":53},"(\\ldots)^2",[20,1304,1306],{"className":1305,"ariaHidden":59},[58],[20,1307,1309,1313,1316,1319],{"className":1308},[63],[20,1310],{"className":1311,"style":1312},[67],"height:1.0641em;vertical-align:-0.25em;",[20,1314,180],{"className":1315},[211],[20,1317,1293],{"className":1318},[584],[20,1320,1322,1325],{"className":1321},[229],[20,1323,190],{"className":1324},[229],[20,1326,1328],{"className":1327},[511],[20,1329,1331],{"className":1330},[417],[20,1332,1334],{"className":1333},[422],[20,1335,1338],{"className":1336,"style":1337},[426],"height:0.8141em;",[20,1339,1340,1343],{"style":675},[20,1341],{"className":1342,"style":528},[434],[20,1344,1346],{"className":1345},[439,440,441,442],[20,1347,269],{"className":1348},[72,442],[1053,1350,1351],{"align":1039},"squared error",[1053,1353,1354],{"align":1039},"removes the sign (missing high or low weighs the same) and punishes big misses much harder",[1034,1356,1357,1473,1476],{},[1053,1358,1359],{"align":1039},[20,1360,1362,1394],{"className":1361},[23],[20,1363,1365],{"className":1364},[27],[29,1366,1367],{"xmlns":31},[33,1368,1369,1391],{},[36,1370,1371],{},[274,1372,1373,1375,1383],{},[43,1374,278],{},[36,1376,1377,1379,1381],{},[39,1378,283],{},[43,1380,45],{},[47,1382,288],{},[36,1384,1385,1387,1389],{},[39,1386,272],{},[43,1388,295],{},[47,1390,264],{},[51,1392,1393],{"encoding":53},"\\sum_{i=0}^{m-1}",[20,1395,1397],{"className":1396,"ariaHidden":59},[58],[20,1398,1400,1404],{"className":1399},[63],[20,1401],{"className":1402,"style":1403},[67],"height:1.2537em;vertical-align:-0.2997em;",[20,1405,1407,1410],{"className":1406},[501],[20,1408,278],{"className":1409,"style":507},[501,505,506],[20,1411,1413],{"className":1412},[511],[20,1414,1416,1465],{"className":1415},[417,418],[20,1417,1419,1462],{"className":1418},[422],[20,1420,1422,1442],{"className":1421,"style":521},[426],[20,1423,1424,1427],{"style":524},[20,1425],{"className":1426,"style":528},[434],[20,1428,1430],{"className":1429},[439,440,441,442],[20,1431,1433,1436,1439],{"className":1432},[72,442],[20,1434,283],{"className":1435},[72,73,442],[20,1437,45],{"className":1438},[83,442],[20,1440,288],{"className":1441},[72,442],[20,1443,1444,1447],{"style":546},[20,1445],{"className":1446,"style":528},[434],[20,1448,1450],{"className":1449},[439,440,441,442],[20,1451,1453,1456,1459],{"className":1452},[72,442],[20,1454,272],{"className":1455},[72,73,442],[20,1457,295],{"className":1458},[562,442],[20,1460,264],{"className":1461},[72,442],[20,1463,482],{"className":1464},[481],[20,1466,1468],{"className":1467},[422],[20,1469,1471],{"className":1470,"style":575},[426],[20,1472],{},[1053,1474,1475],{"align":1039},"sum",[1053,1477,1478],{"align":1039},"adds up everyone's error",[1034,1480,1481,1589,1592],{},[1053,1482,1483],{"align":1039},[20,1484,1486,1508],{"className":1485},[23],[20,1487,1489],{"className":1488},[27],[29,1490,1491],{"xmlns":31},[33,1492,1493,1505],{},[36,1494,1495],{},[260,1496,1497,1499],{},[47,1498,264],{},[36,1500,1501,1503],{},[47,1502,269],{},[39,1504,272],{},[51,1506,1507],{"encoding":53},"\\frac{1}{2m}",[20,1509,1511],{"className":1510,"ariaHidden":59},[58],[20,1512,1514,1518],{"className":1513},[63],[20,1515],{"className":1516,"style":1517},[67],"height:1.1901em;vertical-align:-0.345em;",[20,1519,1521,1524,1586],{"className":1520},[72],[20,1522],{"className":1523},[211,410],[20,1525,1527],{"className":1526},[260],[20,1528,1530,1578],{"className":1529},[417,418],[20,1531,1533,1575],{"className":1532},[422],[20,1534,1536,1553,1561],{"className":1535,"style":427},[426],[20,1537,1538,1541],{"style":430},[20,1539],{"className":1540,"style":435},[434],[20,1542,1544],{"className":1543},[439,440,441,442],[20,1545,1547,1550],{"className":1546},[72,442],[20,1548,269],{"className":1549},[72,442],[20,1551,272],{"className":1552},[72,73,442],[20,1554,1555,1558],{"style":454},[20,1556],{"className":1557,"style":435},[434],[20,1559],{"className":1560,"style":462},[461],[20,1562,1563,1566],{"style":465},[20,1564],{"className":1565,"style":435},[434],[20,1567,1569],{"className":1568},[439,440,441,442],[20,1570,1572],{"className":1571},[72,442],[20,1573,264],{"className":1574},[72,442],[20,1576,482],{"className":1577},[481],[20,1579,1581],{"className":1580},[422],[20,1582,1584],{"className":1583,"style":489},[426],[20,1585],{},[20,1587],{"className":1588},[229,410],[1053,1590,1591],{"align":1039},"average (with an extra 2)",[1053,1593,1594,1595,1623],{"align":1039},"dividing by ",[20,1596,1598,1611],{"className":1597},[23],[20,1599,1601],{"className":1600},[27],[29,1602,1603],{"xmlns":31},[33,1604,1605,1609],{},[36,1606,1607],{},[39,1608,272],{},[51,1610,272],{"encoding":53},[20,1612,1614],{"className":1613,"ariaHidden":59},[58],[20,1615,1617,1620],{"className":1616},[63],[20,1618],{"className":1619,"style":68},[67],[20,1621,272],{"className":1622},[72,73]," turns the sum into an average (otherwise the cost would only grow from having more data, even for an equally good fit), and the 2 is just a bit of bookkeeping that makes the next post's math cleaner, more on that there",[11,1625,1626],{},"Two things fall right out of this formula, and I didn't have to memorize any of it:",[1628,1629,1630,1690],"ol",{},[1631,1632,1633,1689],"li",{},[20,1634,1636,1659],{"className":1635},[23],[20,1637,1639],{"className":1638},[27],[29,1640,1641],{"xmlns":31},[33,1642,1643,1657],{},[36,1644,1645,1647,1649,1651,1653,1655],{},[39,1646,176],{},[43,1648,180],{"stretchy":179},[39,1650,41],{},[43,1652,185],{"separator":59},[39,1654,112],{},[43,1656,190],{"stretchy":179},[51,1658,193],{"encoding":53},[20,1660,1662],{"className":1661,"ariaHidden":59},[58],[20,1663,1665,1668,1671,1674,1677,1680,1683,1686],{"className":1664},[63],[20,1666],{"className":1667,"style":203},[67],[20,1669,176],{"className":1670,"style":207},[72,73],[20,1672,180],{"className":1673},[211],[20,1675,41],{"className":1676,"style":74},[72,73],[20,1678,185],{"className":1679},[218],[20,1681],{"className":1682,"style":222},[78],[20,1684,112],{"className":1685},[72,73],[20,1687,190],{"className":1688},[229]," is never negative. It's a sum of squared terms, so the smallest possible value is zero.",[1631,1691,1692,1771],{},[20,1693,1695,1723],{"className":1694},[23],[20,1696,1698],{"className":1697},[27],[29,1699,1700],{"xmlns":31},[33,1701,1702,1720],{},[36,1703,1704,1706,1708,1710,1712,1714,1716,1718],{},[39,1705,176],{},[43,1707,180],{"stretchy":179},[39,1709,41],{},[43,1711,185],{"separator":59},[39,1713,112],{},[43,1715,190],{"stretchy":179},[43,1717,45],{},[47,1719,288],{},[51,1721,1722],{"encoding":53},"J(w,b) = 0",[20,1724,1726,1762],{"className":1725,"ariaHidden":59},[58],[20,1727,1729,1732,1735,1738,1741,1744,1747,1750,1753,1756,1759],{"className":1728},[63],[20,1730],{"className":1731,"style":203},[67],[20,1733,176],{"className":1734,"style":207},[72,73],[20,1736,180],{"className":1737},[211],[20,1739,41],{"className":1740,"style":74},[72,73],[20,1742,185],{"className":1743},[218],[20,1745],{"className":1746,"style":222},[78],[20,1748,112],{"className":1749},[72,73],[20,1751,190],{"className":1752},[229],[20,1754],{"className":1755,"style":79},[78],[20,1757,45],{"className":1758},[83],[20,1760],{"className":1761,"style":79},[78],[20,1763,1765,1768],{"className":1764},[63],[20,1766],{"className":1767,"style":93},[67],[20,1769,288],{"className":1770},[72]," means a perfect fit. The line passes exactly through every point.",[11,1773,1774,1775,1804],{},"And why square the error instead of, say, taking its absolute value? Because squaring disproportionately punishes big misses. Missing by twice as much costs four times as much in ",[20,1776,1778,1791],{"className":1777},[23],[20,1779,1781],{"className":1780},[27],[29,1782,1783],{"xmlns":31},[33,1784,1785,1789],{},[36,1786,1787],{},[39,1788,176],{},[51,1790,176],{"encoding":53},[20,1792,1794],{"className":1793,"ariaHidden":59},[58],[20,1795,1797,1801],{"className":1796},[63],[20,1798],{"className":1799,"style":1800},[67],"height:0.6833em;",[20,1802,176],{"className":1803,"style":207},[72,73],", not twice as much. That makes the model hate really bad predictions in a way absolute value wouldn't, and it's exactly this behavior that gives the cost its U shape, which we'll see in a moment. Back to the target-shooting contest: it's as if the judge scored exponentially worse the farther the arrow lands from the bullseye. Missing by a little barely stings, missing badly stings way more than twice as much.",[781,1806,1808],{"id":1807},"putting-this-into-code","Putting this into code",[1810,1811,1816],"pre",{"className":1812,"code":1813,"language":1814,"meta":1815,"style":1815},"language-python shiki shiki-themes github-light github-dark","def compute_cost(x, y, w, b):\n    \"\"\"\n    Computes the cost function for linear regression.\n\n    Args:\n      x (ndarray (m,)) : input data, m examples\n      y (ndarray (m,)) : target values\n      w, b (scalar)    : model parameters\n\n    Returns:\n      total_cost (float): the cost of using w and b as parameters to fit\n                          the points (x, y)\n    \"\"\"\n    m = x.shape[0]\n\n    cost_sum = 0\n\n    for i in range(m):\n        f_wb = w * x[i] + b           # model's prediction for example i\n        cost = (f_wb - y[i]) ** 2     # example i's error, squared\n        cost_sum = cost_sum + cost    # add it to the total\n\n    total_cost = (1 \u002F (2 * m)) * cost_sum\n\n    return total_cost\n","python","",[1817,1818,1819,1826,1832,1838,1845,1851,1857,1863,1869,1874,1880,1886,1892,1897,1903,1908,1914,1919,1925,1931,1937,1943,1948,1954,1959],"code",{"__ignoreMap":1815},[20,1820,1823],{"class":1821,"line":1822},"line",1,[20,1824,1825],{},"def compute_cost(x, y, w, b):\n",[20,1827,1829],{"class":1821,"line":1828},2,[20,1830,1831],{},"    \"\"\"\n",[20,1833,1835],{"class":1821,"line":1834},3,[20,1836,1837],{},"    Computes the cost function for linear regression.\n",[20,1839,1841],{"class":1821,"line":1840},4,[20,1842,1844],{"emptyLinePlaceholder":1843},true,"\n",[20,1846,1848],{"class":1821,"line":1847},5,[20,1849,1850],{},"    Args:\n",[20,1852,1854],{"class":1821,"line":1853},6,[20,1855,1856],{},"      x (ndarray (m,)) : input data, m examples\n",[20,1858,1860],{"class":1821,"line":1859},7,[20,1861,1862],{},"      y (ndarray (m,)) : target values\n",[20,1864,1866],{"class":1821,"line":1865},8,[20,1867,1868],{},"      w, b (scalar)    : model parameters\n",[20,1870,1872],{"class":1821,"line":1871},9,[20,1873,1844],{"emptyLinePlaceholder":1843},[20,1875,1877],{"class":1821,"line":1876},10,[20,1878,1879],{},"    Returns:\n",[20,1881,1883],{"class":1821,"line":1882},11,[20,1884,1885],{},"      total_cost (float): the cost of using w and b as parameters to fit\n",[20,1887,1889],{"class":1821,"line":1888},12,[20,1890,1891],{},"                          the points (x, y)\n",[20,1893,1895],{"class":1821,"line":1894},13,[20,1896,1831],{},[20,1898,1900],{"class":1821,"line":1899},14,[20,1901,1902],{},"    m = x.shape[0]\n",[20,1904,1906],{"class":1821,"line":1905},15,[20,1907,1844],{"emptyLinePlaceholder":1843},[20,1909,1911],{"class":1821,"line":1910},16,[20,1912,1913],{},"    cost_sum = 0\n",[20,1915,1917],{"class":1821,"line":1916},17,[20,1918,1844],{"emptyLinePlaceholder":1843},[20,1920,1922],{"class":1821,"line":1921},18,[20,1923,1924],{},"    for i in range(m):\n",[20,1926,1928],{"class":1821,"line":1927},19,[20,1929,1930],{},"        f_wb = w * x[i] + b           # model's prediction for example i\n",[20,1932,1934],{"class":1821,"line":1933},20,[20,1935,1936],{},"        cost = (f_wb - y[i]) ** 2     # example i's error, squared\n",[20,1938,1940],{"class":1821,"line":1939},21,[20,1941,1942],{},"        cost_sum = cost_sum + cost    # add it to the total\n",[20,1944,1946],{"class":1821,"line":1945},22,[20,1947,1844],{"emptyLinePlaceholder":1843},[20,1949,1951],{"class":1821,"line":1950},23,[20,1952,1953],{},"    total_cost = (1 \u002F (2 * m)) * cost_sum\n",[20,1955,1957],{"class":1821,"line":1956},24,[20,1958,1844],{"emptyLinePlaceholder":1843},[20,1960,1962],{"class":1821,"line":1961},25,[20,1963,1964],{},"    return total_cost\n",[11,1966,1967,1968,1971,1972,1975,1976,1979],{},"Notice it's the same ",[1817,1969,1970],{},"for"," loop from ",[14,1973,1974],{"href":16},"the last post's"," ",[1817,1977,1978],{},"compute_model_output",", except instead of storing the predictions, we accumulate each one's squared error. Same structure, different purpose.",[781,1981,1983],{"id":1982},"freezing-one-parameter-to-see-the-other","Freezing one parameter to see the other",[11,1985,1986,1987,97,2015,2043,2044,2094,2095,230],{},"With two parameters to move (",[20,1988,1990,2003],{"className":1989},[23],[20,1991,1993],{"className":1992},[27],[29,1994,1995],{"xmlns":31},[33,1996,1997,2001],{},[36,1998,1999],{},[39,2000,41],{},[51,2002,41],{"encoding":53},[20,2004,2006],{"className":2005,"ariaHidden":59},[58],[20,2007,2009,2012],{"className":2008},[63],[20,2010],{"className":2011,"style":68},[67],[20,2013,41],{"className":2014,"style":74},[72,73],[20,2016,2018,2031],{"className":2017},[23],[20,2019,2021],{"className":2020},[27],[29,2022,2023],{"xmlns":31},[33,2024,2025,2029],{},[36,2026,2027],{},[39,2028,112],{},[51,2030,112],{"encoding":53},[20,2032,2034],{"className":2033,"ariaHidden":59},[58],[20,2035,2037,2040],{"className":2036},[63],[20,2038],{"className":2039,"style":130},[67],[20,2041,112],{"className":2042},[72,73],"), I had a hard time visualizing the cost directly. Classic trick: freeze one and look only at the other. Fix ",[20,2045,2047,2064],{"className":2046},[23],[20,2048,2050],{"className":2049},[27],[29,2051,2052],{"xmlns":31},[33,2053,2054,2062],{},[36,2055,2056,2058,2060],{},[39,2057,112],{},[43,2059,45],{},[47,2061,117],{},[51,2063,120],{"encoding":53},[20,2065,2067,2085],{"className":2066,"ariaHidden":59},[58],[20,2068,2070,2073,2076,2079,2082],{"className":2069},[63],[20,2071],{"className":2072,"style":130},[67],[20,2074,112],{"className":2075},[72,73],[20,2077],{"className":2078,"style":79},[78],[20,2080,45],{"className":2081},[83],[20,2083],{"className":2084,"style":79},[78],[20,2086,2088,2091],{"className":2087},[63],[20,2089],{"className":2090,"style":93},[67],[20,2092,117],{"className":2093},[72]," and move only ",[20,2096,2098,2111],{"className":2097},[23],[20,2099,2101],{"className":2100},[27],[29,2102,2103],{"xmlns":31},[33,2104,2105,2109],{},[36,2106,2107],{},[39,2108,41],{},[51,2110,41],{"encoding":53},[20,2112,2114],{"className":2113,"ariaHidden":59},[58],[20,2115,2117,2120],{"className":2116},[63],[20,2118],{"className":2119,"style":68},[67],[20,2121,41],{"className":2122,"style":74},[72,73],[11,2124,2125,2126,2170],{},"Drag the slider below and watch two things at once: how the line (left panel) gets closer to or farther from the points, and where the red dot sits on the ",[20,2127,2129,2149],{"className":2128},[23],[20,2130,2132],{"className":2131},[27],[29,2133,2134],{"xmlns":31},[33,2135,2136,2146],{},[36,2137,2138,2140,2142,2144],{},[39,2139,176],{},[43,2141,180],{"stretchy":179},[39,2143,41],{},[43,2145,190],{"stretchy":179},[51,2147,2148],{"encoding":53},"J(w)",[20,2150,2152],{"className":2151,"ariaHidden":59},[58],[20,2153,2155,2158,2161,2164,2167],{"className":2154},[63],[20,2156],{"className":2157,"style":203},[67],[20,2159,176],{"className":2160,"style":207},[72,73],[20,2162,180],{"className":2163},[211],[20,2165,41],{"className":2166,"style":74},[72,73],[20,2168,190],{"className":2169},[229]," curve (right panel). As the fit improves, the dot moves down the curve.",[2172,2173],"cost-intuition-explorer",{":b-fixed":117,":initial-w":2174,":w-max":2175,":w-min":288,":x-train":2176,":y-train":2177,"dataLabel":2178,"prediction-label":2179,"w-label":41,"x-label":2180,"y-label":2181},"150","400","[1, 2]","[300, 500]","Actual values","Our prediction","Size (1000 sqft)","Price (1000 dollars)",[11,2183,2184],{},"Notice three things:",[2186,2187,2188,2300,2335],"ul",{},[1631,2189,2190,2244,2245,2248,2249,2299],{},[2191,2192,2193,2194],"strong",{},"The cost is at its lowest exactly at ",[20,2195,2197,2214],{"className":2196},[23],[20,2198,2200],{"className":2199},[27],[29,2201,2202],{"xmlns":31},[33,2203,2204,2212],{},[36,2205,2206,2208,2210],{},[39,2207,41],{},[43,2209,45],{},[47,2211,49],{},[51,2213,54],{"encoding":53},[20,2215,2217,2235],{"className":2216,"ariaHidden":59},[58],[20,2218,2220,2223,2226,2229,2232],{"className":2219},[63],[20,2221],{"className":2222,"style":68},[67],[20,2224,41],{"className":2225,"style":74},[72,73],[20,2227],{"className":2228,"style":79},[78],[20,2230,45],{"className":2231},[83],[20,2233],{"className":2234,"style":79},[78],[20,2236,2238,2241],{"className":2237},[63],[20,2239],{"className":2240,"style":93},[67],[20,2242,49],{"className":2243},[72],", the same value you found by eye in ",[14,2246,2247],{"href":16},"the last post",". With ",[20,2250,2252,2269],{"className":2251},[23],[20,2253,2255],{"className":2254},[27],[29,2256,2257],{"xmlns":31},[33,2258,2259,2267],{},[36,2260,2261,2263,2265],{},[39,2262,112],{},[43,2264,45],{},[47,2266,117],{},[51,2268,120],{"encoding":53},[20,2270,2272,2290],{"className":2271,"ariaHidden":59},[58],[20,2273,2275,2278,2281,2284,2287],{"className":2274},[63],[20,2276],{"className":2277,"style":130},[67],[20,2279,112],{"className":2280},[72,73],[20,2282],{"className":2283,"style":79},[78],[20,2285,45],{"className":2286},[83],[20,2288],{"className":2289,"style":79},[78],[20,2291,2293,2296],{"className":2292},[63],[20,2294],{"className":2295,"style":93},[67],[20,2297,117],{"className":2298},[72],", the cost there hits zero, because the line passes through both points.",[1631,2301,2302,2305,2306,2334],{},[2191,2303,2304],{},"The cost shoots up fast"," when ",[20,2307,2309,2322],{"className":2308},[23],[20,2310,2312],{"className":2311},[27],[29,2313,2314],{"xmlns":31},[33,2315,2316,2320],{},[36,2317,2318],{},[39,2319,41],{},[51,2321,41],{"encoding":53},[20,2323,2325],{"className":2324,"ariaHidden":59},[58],[20,2326,2328,2331],{"className":2327},[63],[20,2329],{"className":2330,"style":68},[67],[20,2332,41],{"className":2333,"style":74},[72,73]," gets too big or too small. That's the square in the formula doing its job.",[1631,2336,2337,2340,2341,97,2369,2397,2398,2426],{},[2191,2338,2339],{},"That's when it clicked: minimizing the cost is the same thing as finding the best fit."," It's not a coincidence, it's the definition. Choosing ",[20,2342,2344,2357],{"className":2343},[23],[20,2345,2347],{"className":2346},[27],[29,2348,2349],{"xmlns":31},[33,2350,2351,2355],{},[36,2352,2353],{},[39,2354,41],{},[51,2356,41],{"encoding":53},[20,2358,2360],{"className":2359,"ariaHidden":59},[58],[20,2361,2363,2366],{"className":2362},[63],[20,2364],{"className":2365,"style":68},[67],[20,2367,41],{"className":2368,"style":74},[72,73],[20,2370,2372,2385],{"className":2371},[23],[20,2373,2375],{"className":2374},[27],[29,2376,2377],{"xmlns":31},[33,2378,2379,2383],{},[36,2380,2381],{},[39,2382,112],{},[51,2384,112],{"encoding":53},[20,2386,2388],{"className":2387,"ariaHidden":59},[58],[20,2389,2391,2394],{"className":2390},[63],[20,2392],{"className":2393,"style":130},[67],[20,2395,112],{"className":2396},[72,73]," that minimize ",[20,2399,2401,2414],{"className":2400},[23],[20,2402,2404],{"className":2403},[27],[29,2405,2406],{"xmlns":31},[33,2407,2408,2412],{},[36,2409,2410],{},[39,2411,176],{},[51,2413,176],{"encoding":53},[20,2415,2417],{"className":2416,"ariaHidden":59},[58],[20,2418,2420,2423],{"className":2419},[63],[20,2421],{"className":2422,"style":1800},[67],[20,2424,176],{"className":2425,"style":207},[72,73]," is literally what we mean by training a model.",[11,2428,2429,2430,2481],{},"Try dragging the slider to ",[20,2431,2433,2451],{"className":2432},[23],[20,2434,2436],{"className":2435},[27],[29,2437,2438],{"xmlns":31},[33,2439,2440,2448],{},[36,2441,2442,2444,2446],{},[39,2443,41],{},[43,2445,45],{},[47,2447,288],{},[51,2449,2450],{"encoding":53},"w = 0",[20,2452,2454,2472],{"className":2453,"ariaHidden":59},[58],[20,2455,2457,2460,2463,2466,2469],{"className":2456},[63],[20,2458],{"className":2459,"style":68},[67],[20,2461,41],{"className":2462,"style":74},[72,73],[20,2464],{"className":2465,"style":79},[78],[20,2467,45],{"className":2468},[83],[20,2470],{"className":2471,"style":79},[78],[20,2473,2475,2478],{"className":2474},[63],[20,2476],{"className":2477,"style":93},[67],[20,2479,288],{"className":2480},[72],": the line goes flat (predicts the same price for any house size) and the cost spikes.",[781,2483,2485],{"id":2484},"the-real-world-doesnt-hand-you-2-perfect-points","The real world doesn't hand you 2 perfect points",[11,2487,2488],{},"So far, with 2 points and 2 parameters, I could get the cost to hit zero. That's rare. Let's swap the training set for a more realistic one, 6 houses, where the prices don't fall exactly on any single line (real noise, like similar houses selling for slightly different prices):",[1028,2490,2491,2500],{},[1031,2492,2493],{},[1034,2494,2495,2498],{},[1037,2496,2180],{"align":2497},"center",[1037,2499,2181],{"align":2497},[1048,2501,2502,2510,2518,2526,2534,2542],{},[1034,2503,2504,2507],{},[1053,2505,2506],{"align":2497},"1.0",[1053,2508,2509],{"align":2497},"250",[1034,2511,2512,2515],{},[1053,2513,2514],{"align":2497},"1.7",[1053,2516,2517],{"align":2497},"300",[1034,2519,2520,2523],{},[1053,2521,2522],{"align":2497},"2.0",[1053,2524,2525],{"align":2497},"480",[1034,2527,2528,2531],{},[1053,2529,2530],{"align":2497},"2.5",[1053,2532,2533],{"align":2497},"430",[1034,2535,2536,2539],{},[1053,2537,2538],{"align":2497},"3.0",[1053,2540,2541],{"align":2497},"630",[1034,2543,2544,2547],{},[1053,2545,2546],{"align":2497},"3.2",[1053,2548,2549],{"align":2497},"730",[11,2551,2552,2553,2605],{},"The open question: is there still some ",[20,2554,2556,2578],{"className":2555},[23],[20,2557,2559],{"className":2558},[27],[29,2560,2561],{"xmlns":31},[33,2562,2563,2575],{},[36,2564,2565,2567,2569,2571,2573],{},[43,2566,180],{"stretchy":179},[39,2568,41],{},[43,2570,185],{"separator":59},[39,2572,112],{},[43,2574,190],{"stretchy":179},[51,2576,2577],{"encoding":53},"(w, b)",[20,2579,2581],{"className":2580,"ariaHidden":59},[58],[20,2582,2584,2587,2590,2593,2596,2599,2602],{"className":2583},[63],[20,2585],{"className":2586,"style":203},[67],[20,2588,180],{"className":2589},[211],[20,2591,41],{"className":2592,"style":74},[72,73],[20,2594,185],{"className":2595},[218],[20,2597],{"className":2598,"style":222},[78],[20,2600,112],{"className":2601},[72,73],[20,2603,190],{"className":2604},[229]," that zeroes out the cost here?",[11,2607,2608,2609,1975,2637,1975,2640,2668],{},"Move both sliders below (now ",[20,2610,2612,2625],{"className":2611},[23],[20,2613,2615],{"className":2614},[27],[29,2616,2617],{"xmlns":31},[33,2618,2619,2623],{},[36,2620,2621],{},[39,2622,41],{},[51,2624,41],{"encoding":53},[20,2626,2628],{"className":2627,"ariaHidden":59},[58],[20,2629,2631,2634],{"className":2630},[63],[20,2632],{"className":2633,"style":68},[67],[20,2635,41],{"className":2636,"style":74},[72,73],[2191,2638,2639],{},"and",[20,2641,2643,2656],{"className":2642},[23],[20,2644,2646],{"className":2645},[27],[29,2647,2648],{"xmlns":31},[33,2649,2650,2654],{},[36,2651,2652],{},[39,2653,112],{},[51,2655,112],{"encoding":53},[20,2657,2659],{"className":2658,"ariaHidden":59},[58],[20,2660,2662,2665],{"className":2661},[63],[20,2663],{"className":2664,"style":130},[67],[20,2666,112],{"className":2667},[72,73]," at once) and try to reach the center of the heatmap, where the cost is lowest. Switch to the 3D view and rotate the surface with your finger or mouse to feel the shape of the \"valley\".",[2670,2671],"cost-explorer",{":b-max":49,":b-min":2672,":initial-b":288,":initial-w":2174,":w-max":2175,":w-min":288,":x-train":2673,":y-train":2674,"b-label":112,"dataLabel":2178,"prediction-label":2675,"w-label":41,"x-label":2180,"y-label":2181},"-200","[1.0, 1.7, 2.0, 2.5, 3.0, 3.2]","[250, 300, 480, 430, 630, 730]","Your prediction",[11,2677,2678,2679,2682,2683,97,2737,2789],{},"You probably noticed: this time the cost ",[2191,2680,2681],{},"doesn't"," hit zero. The best you can get lands around ",[20,2684,2686,2706],{"className":2685},[23],[20,2687,2689],{"className":2688},[27],[29,2690,2691],{"xmlns":31},[33,2692,2693,2703],{},[36,2694,2695,2697,2700],{},[39,2696,41],{},[43,2698,2699],{},"≈",[47,2701,2702],{},"209",[51,2704,2705],{"encoding":53},"w \\approx 209",[20,2707,2709,2728],{"className":2708,"ariaHidden":59},[58],[20,2710,2712,2716,2719,2722,2725],{"className":2711},[63],[20,2713],{"className":2714,"style":2715},[67],"height:0.4831em;",[20,2717,41],{"className":2718,"style":74},[72,73],[20,2720],{"className":2721,"style":79},[78],[20,2723,2699],{"className":2724},[83],[20,2726],{"className":2727,"style":79},[78],[20,2729,2731,2734],{"className":2730},[63],[20,2732],{"className":2733,"style":93},[67],[20,2735,2702],{"className":2736},[72],[20,2738,2740,2759],{"className":2739},[23],[20,2741,2743],{"className":2742},[27],[29,2744,2745],{"xmlns":31},[33,2746,2747,2756],{},[36,2748,2749,2751,2753],{},[39,2750,112],{},[43,2752,2699],{},[47,2754,2755],{},"2.4",[51,2757,2758],{"encoding":53},"b \\approx 2.4",[20,2760,2762,2780],{"className":2761,"ariaHidden":59},[58],[20,2763,2765,2768,2771,2774,2777],{"className":2764},[63],[20,2766],{"className":2767,"style":130},[67],[20,2769,112],{"className":2770},[72,73],[20,2772],{"className":2773,"style":79},[78],[20,2775,2699],{"className":2776},[83],[20,2778],{"className":2779,"style":79},[78],[20,2781,2783,2786],{"className":2782},[63],[20,2784],{"className":2785,"style":93},[67],[20,2787,2755],{"className":2788},[72],", with a cost around 1736. Why? Because these 6 houses aren't aligned. No single line passes exactly through all of them. The best fit is the one that leaves the smallest possible total squared error, and that's exactly what the cost function defines as \"the best\".",[11,2791,2792,2793,2796,2797,2801],{},"Hold on to this: ",[2191,2794,2795],{},"a nonzero minimum cost is the normal case",", not a bug. Zero cost tends to happen when you have too little data (like our 2-point case) or when the model memorized the data instead of learning its pattern (that has a name, ",[157,2798,2800],{"definition":2799},"when a model memorizes the training data instead of learning its general pattern, and so performs poorly on new data","overfitting",", but that's a topic for further down the course).",[781,2803,2805],{"id":2804},"why-the-surface-is-always-a-bowl","Why the surface is always a bowl",[11,2807,2808,2809,2813],{},"Rotating the 3D surface above, you probably noticed it always looks like a soup bowl, a single valley, no fake peaks along the way. That's not a coincidence of our specific example, it's a direct consequence of squaring the error in the formula. Any time you square something, the result is a ",[157,2810,2812],{"definition":2811},"a bowl-shaped surface with a single valley, no fake minima where a search algorithm could get stuck","convex"," surface.",[11,2815,2816,2817,2845,2846,2874,2875,2988],{},"To see this more cleanly, without the scale distortion the real data brings (notice ",[20,2818,2820,2833],{"className":2819},[23],[20,2821,2823],{"className":2822},[27],[29,2824,2825],{"xmlns":31},[33,2826,2827,2831],{},[36,2828,2829],{},[39,2830,41],{},[51,2832,41],{"encoding":53},[20,2834,2836],{"className":2835,"ariaHidden":59},[58],[20,2837,2839,2842],{"className":2838},[63],[20,2840],{"className":2841,"style":68},[67],[20,2843,41],{"className":2844,"style":74},[72,73]," ranges from 0 to 400 and ",[20,2847,2849,2862],{"className":2848},[23],[20,2850,2852],{"className":2851},[27],[29,2853,2854],{"xmlns":31},[33,2855,2856,2860],{},[36,2857,2858],{},[39,2859,112],{},[51,2861,112],{"encoding":53},[20,2863,2865],{"className":2864,"ariaHidden":59},[58],[20,2866,2868,2871],{"className":2867},[63],[20,2869],{"className":2870,"style":130},[67],[20,2872,112],{"className":2873},[72,73]," from -200 to 200 in the chart above, which stretches the valley), here's the idealized version of the same shape, just ",[20,2876,2878,2905],{"className":2877},[23],[20,2879,2881],{"className":2880},[27],[29,2882,2883],{"xmlns":31},[33,2884,2885,2902],{},[36,2886,2887,2893,2896],{},[299,2888,2889,2891],{},[39,2890,41],{},[47,2892,269],{},[43,2894,2895],{},"+",[299,2897,2898,2900],{},[39,2899,112],{},[47,2901,269],{},[51,2903,2904],{"encoding":53},"w^2 + b^2",[20,2906,2908,2953],{"className":2907,"ariaHidden":59},[58],[20,2909,2911,2915,2944,2947,2950],{"className":2910},[63],[20,2912],{"className":2913,"style":2914},[67],"height:0.8974em;vertical-align:-0.0833em;",[20,2916,2918,2921],{"className":2917},[72],[20,2919,41],{"className":2920,"style":74},[72,73],[20,2922,2924],{"className":2923},[511],[20,2925,2927],{"className":2926},[417],[20,2928,2930],{"className":2929},[422],[20,2931,2933],{"className":2932,"style":1337},[426],[20,2934,2935,2938],{"style":675},[20,2936],{"className":2937,"style":528},[434],[20,2939,2941],{"className":2940},[439,440,441,442],[20,2942,269],{"className":2943},[72,442],[20,2945],{"className":2946,"style":700},[78],[20,2948,2895],{"className":2949},[562],[20,2951],{"className":2952,"style":700},[78],[20,2954,2956,2959],{"className":2955},[63],[20,2957],{"className":2958,"style":1337},[67],[20,2960,2962,2965],{"className":2961},[72],[20,2963,112],{"className":2964},[72,73],[20,2966,2968],{"className":2967},[511],[20,2969,2971],{"className":2970},[417],[20,2972,2974],{"className":2973},[422],[20,2975,2977],{"className":2976,"style":1337},[426],[20,2978,2979,2982],{"style":675},[20,2980],{"className":2981,"style":528},[434],[20,2983,2985],{"className":2984},[439,440,441,442],[20,2986,269],{"className":2987},[72,442],", with both axes on the same scale:",[2990,2991],"cost-surface3d",{":b-max":2992,":b-min":2993,":w-max":2992,":w-min":2993},"20","-20",[11,2995,2996,2997,3000],{},"Why does this shape matter so much? Because a convex surface guarantees there's only ",[2191,2998,2999],{},"one"," minimum, the global minimum. There's no hidden valley elsewhere for a search algorithm to fall into and get stuck, mistakenly thinking it already reached the bottom when it hasn't. That guarantee is what makes the next step of the course reliable: an automatic way to walk down to the bottom of that bowl, without you having to drag a slider for the rest of your life.",[781,3002,3004],{"id":3003},"wrapping-up","Wrapping up",[1028,3006,3007,3017],{},[1031,3008,3009],{},[1034,3010,3011,3014],{},[1037,3012,3013],{"align":1039},"Concept",[1037,3015,3016],{"align":1039},"What we established",[1048,3018,3019,3083,3091,3161,3169,3286],{},[1034,3020,3021,3024],{},[1053,3022,3023],{"align":1039},"Cost function",[1053,3025,3026,3082],{"align":1039},[20,3027,3029,3052],{"className":3028},[23],[20,3030,3032],{"className":3031},[27],[29,3033,3034],{"xmlns":31},[33,3035,3036,3050],{},[36,3037,3038,3040,3042,3044,3046,3048],{},[39,3039,176],{},[43,3041,180],{"stretchy":179},[39,3043,41],{},[43,3045,185],{"separator":59},[39,3047,112],{},[43,3049,190],{"stretchy":179},[51,3051,193],{"encoding":53},[20,3053,3055],{"className":3054,"ariaHidden":59},[58],[20,3056,3058,3061,3064,3067,3070,3073,3076,3079],{"className":3057},[63],[20,3059],{"className":3060,"style":203},[67],[20,3062,176],{"className":3063,"style":207},[72,73],[20,3065,180],{"className":3066},[211],[20,3068,41],{"className":3069,"style":74},[72,73],[20,3071,185],{"className":3072},[218],[20,3074],{"className":3075,"style":222},[78],[20,3077,112],{"className":3078},[72,73],[20,3080,190],{"className":3081},[229],", a number that measures how wrong the predictions are on the training data",[1034,3084,3085,3088],{},[1053,3086,3087],{"align":1039},"Why squared",[1053,3089,3090],{"align":1039},"removes the sign of the error and heavily punishes big misses, plus guarantees a convex surface",[1034,3092,3093,3130],{},[1053,3094,3095,3096],{"align":1039},"Why ",[20,3097,3099,3115],{"className":3098},[23],[20,3100,3102],{"className":3101},[27],[29,3103,3104],{"xmlns":31},[33,3105,3106,3112],{},[36,3107,3108,3110],{},[47,3109,269],{},[39,3111,272],{},[51,3113,3114],{"encoding":53},"2m",[20,3116,3118],{"className":3117,"ariaHidden":59},[58],[20,3119,3121,3124,3127],{"className":3120},[63],[20,3122],{"className":3123,"style":93},[67],[20,3125,269],{"className":3126},[72],[20,3128,272],{"className":3129},[72,73],[1053,3131,3132,3160],{"align":1039},[20,3133,3135,3148],{"className":3134},[23],[20,3136,3138],{"className":3137},[27],[29,3139,3140],{"xmlns":31},[33,3141,3142,3146],{},[36,3143,3144],{},[39,3145,272],{},[51,3147,272],{"encoding":53},[20,3149,3151],{"className":3150,"ariaHidden":59},[58],[20,3152,3154,3157],{"className":3153},[63],[20,3155],{"className":3156,"style":68},[67],[20,3158,272],{"className":3159},[72,73]," turns the sum into an average, and the 2 simplifies the math coming in the next post",[1034,3162,3163,3166],{},[1053,3164,3165],{"align":1039},"Shape",[1053,3167,3168],{"align":1039},"a U curve (one parameter) and a soup bowl (two parameters)",[1034,3170,3171,3174],{},[1053,3172,3173],{"align":1039},"Training the model",[1053,3175,3176,3177,3229,3230],{"align":1039},"means finding the pair ",[20,3178,3180,3202],{"className":3179},[23],[20,3181,3183],{"className":3182},[27],[29,3184,3185],{"xmlns":31},[33,3186,3187,3199],{},[36,3188,3189,3191,3193,3195,3197],{},[43,3190,180],{"stretchy":179},[39,3192,41],{},[43,3194,185],{"separator":59},[39,3196,112],{},[43,3198,190],{"stretchy":179},[51,3200,3201],{"encoding":53},"(w,b)",[20,3203,3205],{"className":3204,"ariaHidden":59},[58],[20,3206,3208,3211,3214,3217,3220,3223,3226],{"className":3207},[63],[20,3209],{"className":3210,"style":203},[67],[20,3212,180],{"className":3213},[211],[20,3215,41],{"className":3216,"style":74},[72,73],[20,3218,185],{"className":3219},[218],[20,3221],{"className":3222,"style":222},[78],[20,3224,112],{"className":3225},[72,73],[20,3227,190],{"className":3228},[229]," that minimizes ",[20,3231,3233,3256],{"className":3232},[23],[20,3234,3236],{"className":3235},[27],[29,3237,3238],{"xmlns":31},[33,3239,3240,3254],{},[36,3241,3242,3244,3246,3248,3250,3252],{},[39,3243,176],{},[43,3245,180],{"stretchy":179},[39,3247,41],{},[43,3249,185],{"separator":59},[39,3251,112],{},[43,3253,190],{"stretchy":179},[51,3255,193],{"encoding":53},[20,3257,3259],{"className":3258,"ariaHidden":59},[58],[20,3260,3262,3265,3268,3271,3274,3277,3280,3283],{"className":3261},[63],[20,3263],{"className":3264,"style":203},[67],[20,3266,176],{"className":3267,"style":207},[72,73],[20,3269,180],{"className":3270},[211],[20,3272,41],{"className":3273,"style":74},[72,73],[20,3275,185],{"className":3276},[218],[20,3278],{"className":3279,"style":222},[78],[20,3281,112],{"className":3282},[72,73],[20,3284,190],{"className":3285},[229],[1034,3287,3288,3291],{},[1053,3289,3290],{"align":1039},"Nonzero minimum cost",[1053,3292,3293],{"align":1039},"normal when the data has noise, not a sign something's wrong",[11,3295,3296,3303],{},[2191,3297,3298,3302],{},[14,3299,3301],{"href":3300},"\u002Fen\u002Fplaylists\u002Fmachine-learning-specialization\u002Flab04-gradient-descent","Coming up next",":"," you've now felt firsthand what it's like to hunt for the bottom of the bowl by dragging sliders. It doesn't scale. The next post introduces gradient descent, an algorithm that uses the slope of the cost surface to walk down to the bottom on its own, with no guessing required from you.",[781,3305,3307],{"id":3306},"practical-application","Practical application",[11,3309,3310,3311,3313,3314,3320,3321,230],{},"Same real housing dataset from ",[14,3312,2247],{"href":16}," (500 houses, ",[14,3315,3319],{"href":3316,"rel":3317},"https:\u002F\u002Fwww.kaggle.com\u002Fdatasets\u002Fdenkuznetz\u002Fhousing-prices-regression",[3318],"nofollow","Housing Prices Regression, Kaggle","). Now with a real ",[1817,3322,3323],{},"compute_cost",[1810,3325,3327],{"className":1812,"code":3326,"language":1814,"meta":1815,"style":1815},"def compute_cost(x, y, w, b):\n    m = x.shape[0]\n    cost_sum = 0\n    for i in range(m):\n        f_wb = w * x[i] + b\n        cost_sum += (f_wb - y[i]) ** 2\n    return (1 \u002F (2 * m)) * cost_sum\n\n# x_sqft already in \"hundreds of sqft\", y_price already in \"thousands of dollars\"\n# (same 50-house sample from the last post)\n\nprint(\"cost with a bad guess, w=50, b=100:\", compute_cost(x_sqft, y_price, 50, 100))\nprint(\"cost near the optimal fit, w=116, b=399:\", compute_cost(x_sqft, y_price, 116, 399))\n",[1817,3328,3329,3333,3337,3341,3345,3350,3355,3360,3364,3369,3374,3378,3383],{"__ignoreMap":1815},[20,3330,3331],{"class":1821,"line":1822},[20,3332,1825],{},[20,3334,3335],{"class":1821,"line":1828},[20,3336,1902],{},[20,3338,3339],{"class":1821,"line":1834},[20,3340,1913],{},[20,3342,3343],{"class":1821,"line":1840},[20,3344,1924],{},[20,3346,3347],{"class":1821,"line":1847},[20,3348,3349],{},"        f_wb = w * x[i] + b\n",[20,3351,3352],{"class":1821,"line":1853},[20,3353,3354],{},"        cost_sum += (f_wb - y[i]) ** 2\n",[20,3356,3357],{"class":1821,"line":1859},[20,3358,3359],{},"    return (1 \u002F (2 * m)) * cost_sum\n",[20,3361,3362],{"class":1821,"line":1865},[20,3363,1844],{"emptyLinePlaceholder":1843},[20,3365,3366],{"class":1821,"line":1871},[20,3367,3368],{},"# x_sqft already in \"hundreds of sqft\", y_price already in \"thousands of dollars\"\n",[20,3370,3371],{"class":1821,"line":1876},[20,3372,3373],{},"# (same 50-house sample from the last post)\n",[20,3375,3376],{"class":1821,"line":1882},[20,3377,1844],{"emptyLinePlaceholder":1843},[20,3379,3380],{"class":1821,"line":1888},[20,3381,3382],{},"print(\"cost with a bad guess, w=50, b=100:\", compute_cost(x_sqft, y_price, 50, 100))\n",[20,3384,3385],{"class":1821,"line":1894},[20,3386,3387],{},"print(\"cost near the optimal fit, w=116, b=399:\", compute_cost(x_sqft, y_price, 116, 399))\n",[3389,3390,3391],"blockquote",{},[11,3392,3393,1975,3396,3399,3400],{},[2191,3394,3395],{},"Output:",[1817,3397,3398],{},"cost with a bad guess, w=50, b=100: 90317.6"," \u002F ",[1817,3401,3402],{},"cost near the optimal fit, w=116, b=399: 5189.6",[11,3404,3405],{},"Almost 20 times less cost just from picking better parameters. Move the sliders yourself and try to get close to that ~5189 (notice: you can't hit zero, that really is the real minimum, real data has noise):",[3407,3408],"housing-cost-explorer",{"dataLabel":3409,"prediction-label":2675,"x-label":3410,"y-label":2181},"Real houses","Size (100 sqft)",[3412,3413,3414],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":1815,"searchDepth":1828,"depth":1828,"links":3416},[3417,3418,3419,3420,3421,3422,3423,3424],{"id":783,"depth":1828,"text":784},{"id":795,"depth":1828,"text":796},{"id":1807,"depth":1828,"text":1808},{"id":1982,"depth":1828,"text":1983},{"id":2484,"depth":1828,"text":2485},{"id":2804,"depth":1828,"text":2805},{"id":3003,"depth":1828,"text":3004},{"id":3306,"depth":1828,"text":3307},null,"2026-08-18","How to put a single number on how wrong a model is: J(w,b), why squared error, why divide by 2m, and why the cost surface is always a bowl.","md",{},"\u002Fen\u002Fplaylists\u002Fmachine-learning-specialization\u002Flab03-cost-function","machine-learning-specialization",{"title":6,"description":3427},"published","en\u002Fplaylists\u002Fmachine-learning-specialization\u002Flab03-cost-function",[3436,3437,3438],"cost-function","squared-error","fundamentals","Qjmvwr-oCFxG6bDlDBH6R9_fNvaKZWiy6I6PwELBaBg",1787338984712]