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