[{"data":1,"prerenderedAt":331},["ShallowReactive",2],{"lang-switch-post-\u002Fen\u002Fplaylists\u002Fmachine-learning-specialization\u002Fw3-lab07-scikit-learn":3,"post-en-machine-learning-specialization-w3-lab07-scikit-learn":4},"\u002Fplaylists\u002Fmachine-learning-specialization\u002Fw3-lab07-scikit-learn",{"id":5,"title":6,"body":7,"cover":315,"date":316,"description":317,"extension":318,"meta":319,"navigation":48,"order":320,"path":321,"playlist":322,"seo":323,"status":324,"stem":325,"tags":326,"__hash__":330},"posts\u002Fen\u002Fplaylists\u002Fmachine-learning-specialization\u002Fw3-lab07-scikit-learn.md","What Took Six Labs, Scikit-Learn Does in Three Lines",{"type":8,"value":9,"toc":309},"minimark",[10,20,25,62,83,98,111,120,129,137,141,144,153,162,180,184,235,239,250,274,287,305],[11,12,13,14,19],"p",{},"Six labs to get here: sigmoid, decision boundary, logistic loss, cost, gradient descent, all hand-written, formula by formula. This lab is the shortest of the week, and on purpose: to show all of that ",[15,16,18],"a",{"href":17},"\u002Fen\u002Fplaylists\u002Fmachine-learning-specialization\u002Fw2-lab05-scikit-learn","already exists ready-made"," in a library, exactly like we already saw on the linear regression side.",[21,22,24],"h2",{"id":23},"the-entire-code","The entire code",[26,27,32],"pre",{"className":28,"code":29,"language":30,"meta":31,"style":31},"language-python shiki shiki-themes github-light github-dark","from sklearn.linear_model import LogisticRegression\n\nlr_model = LogisticRegression()\nlr_model.fit(X, y)\n","python","",[33,34,35,43,50,56],"code",{"__ignoreMap":31},[36,37,40],"span",{"class":38,"line":39},"line",1,[36,41,42],{},"from sklearn.linear_model import LogisticRegression\n",[36,44,46],{"class":38,"line":45},2,[36,47,49],{"emptyLinePlaceholder":48},true,"\n",[36,51,53],{"class":38,"line":52},3,[36,54,55],{},"lr_model = LogisticRegression()\n",[36,57,59],{"class":38,"line":58},4,[36,60,61],{},"lr_model.fit(X, y)\n",[11,63,64,67,68,71,72,75,76,75,79,82],{},[33,65,66],{},"LogisticRegression()"," creates the model, ",[33,69,70],{},".fit(X, y)"," trains it. No writing ",[33,73,74],{},"sigmoid",", no writing ",[33,77,78],{},"compute_gradient_logistic",[33,80,81],{},"gradient_descent",". Under the hood, sklearn does all the work the last six labs broke down line by line.",[26,84,86],{"className":28,"code":85,"language":30,"meta":31,"style":31},"y_pred = lr_model.predict(X)\nprint(\"Prediction on training set:\", y_pred)\n",[33,87,88,93],{"__ignoreMap":31},[36,89,90],{"class":38,"line":39},[36,91,92],{},"y_pred = lr_model.predict(X)\n",[36,94,95],{"class":38,"line":45},[36,96,97],{},"print(\"Prediction on training set:\", y_pred)\n",[99,100,101],"blockquote",{},[11,102,103,107,108],{},[104,105,106],"strong",{},"Output:"," ",[33,109,110],{},"Prediction on training set: [0 0 0 1 1 1]",[26,112,114],{"className":28,"code":113,"language":30,"meta":31,"style":31},"print(\"Accuracy on training set:\", lr_model.score(X, y))\n",[33,115,116],{"__ignoreMap":31},[36,117,118],{"class":38,"line":39},[36,119,113],{},[99,121,122],{},[11,123,124,107,126],{},[104,125,106],{},[33,127,128],{},"Accuracy on training set: 1.0",[11,130,131,132,136],{},"On the usual six-patient dataset, it gets everyone right. Not surprising, ",[15,133,135],{"href":134},"\u002Fen\u002Fplaylists\u002Fmachine-learning-specialization\u002Fw3-lab06-gradient-descent","the hand-trained boundary from the last lab"," already did too.",[21,138,140],{"id":139},"the-detail-sklearn-is-hiding-for-now","The detail sklearn is hiding (for now)",[11,142,143],{},"Peek at the weights sklearn found:",[26,145,147],{"className":28,"code":146,"language":30,"meta":31,"style":31},"print(\"coefficients:\", lr_model.coef_, \"intercept:\", lr_model.intercept_)\n",[33,148,149],{"__ignoreMap":31},[36,150,151],{"class":38,"line":39},[36,152,146],{},[99,154,155],{},[11,156,157,107,159],{},[104,158,106],{},[33,160,161],{},"coefficients: [[0.9041 0.7359]] intercept: [-2.3337]",[11,163,164,165,168,169,172,173,175,176,179],{},"Notice these numbers ",[104,166,167],{},"don't match"," the ",[33,170,171],{},"w=[5.2812, 5.0782], b=-14.2224"," plain gradient descent found in the last lab, even though both get all six patients right. That's nobody's bug: sklearn's ",[33,174,66],{},", by default, ships with a built-in brake that penalizes weights for getting too large (this week's closing topic, regularization). It's solving a ",[104,177,178],{},"slightly different"," problem than the raw gradient descent I implemented, which is why the final decision looks similar but the numbers differ. Hold onto that oddity, it'll make full sense two labs from now.",[21,181,183],{"id":182},"wrapping-up","Wrapping up",[185,186,187,201],"table",{},[188,189,190],"thead",{},[191,192,193,198],"tr",{},[194,195,197],"th",{"align":196},"left","What I already knew",[194,199,200],{"align":196},"What this lecture settled",[202,203,204,227],"tbody",{},[191,205,206,210],{},[207,208,209],"td",{"align":196},"I built sigmoid, cost and gradient descent from scratch",[207,211,212,213,216,217,216,220,223,224],{"align":196},"sklearn packages all of it into ",[33,214,215],{},"fit","\u002F",[33,218,219],{},"predict",[33,221,222],{},"score",", ",[15,225,226],{"href":17},"same as linear regression already showed",[191,228,229,232],{},[207,230,231],{"align":196},"The weights I trained by hand solve the problem with no penalty at all",[207,233,234],{"align":196},"sklearn's weights differ by default, because it already regularizes without me asking",[21,236,238],{"id":237},"practical-application","Practical Application",[11,240,241,242,245,246,249],{},"Same real dataset ",[15,243,244],{"href":134},"from the last lab"," (",[33,247,248],{},"worst concave points",", 60-patient sample, z-score normalized):",[26,251,253],{"className":28,"code":252,"language":30,"meta":31,"style":31},"lr_model = LogisticRegression()\nlr_model.fit(X_train_z, y_train)\nprint(\"coefficient:\", lr_model.coef_, \"intercept:\", lr_model.intercept_)\nprint(\"accuracy:\", lr_model.score(X_train_z, y_train))\n",[33,254,255,259,264,269],{"__ignoreMap":31},[36,256,257],{"class":38,"line":39},[36,258,55],{},[36,260,261],{"class":38,"line":45},[36,262,263],{},"lr_model.fit(X_train_z, y_train)\n",[36,265,266],{"class":38,"line":52},[36,267,268],{},"print(\"coefficient:\", lr_model.coef_, \"intercept:\", lr_model.intercept_)\n",[36,270,271],{"class":38,"line":58},[36,272,273],{},"print(\"accuracy:\", lr_model.score(X_train_z, y_train))\n",[99,275,276],{},[11,277,278,107,280,283,284],{},[104,279,106],{},[33,281,282],{},"coefficient: [[2.4562]] intercept: [-0.8278]"," · ",[33,285,286],{},"accuracy: 0.9",[11,288,289,290,293,294,297,298,107,301,304],{},"Same accuracy (",[33,291,292],{},"0.90",") plain gradient descent found on this exact dataset, but sklearn's coefficient (",[33,295,296],{},"2.4562",") is a lot smaller than the ",[33,299,300],{},"w≈4.175",[15,302,303],{"href":134},"I ran by hand",". Two different boundaries, the same final call on these 60 patients: that's regularization in action, shrinking the weight without losing accuracy. The topic two labs from here.",[306,307,308],"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":31,"searchDepth":45,"depth":45,"links":310},[311,312,313,314],{"id":23,"depth":45,"text":24},{"id":139,"depth":45,"text":140},{"id":182,"depth":45,"text":183},{"id":237,"depth":45,"text":238},null,"2026-09-08","Week 3, Lab 7: sigmoid, decision boundary, loss, cost and gradient descent, all hand-written over the last six labs, collapse into import, fit, predict. And the comparison with real sklearn drops a spoiler for the course's next topic.","md",{},18,"\u002Fen\u002Fplaylists\u002Fmachine-learning-specialization\u002Fw3-lab07-scikit-learn","machine-learning-specialization",{"title":6,"description":317},"published","en\u002Fplaylists\u002Fmachine-learning-specialization\u002Fw3-lab07-scikit-learn",[327,328,329],"classification","logistic-regression","scikit-learn","WcOtJMhdeKiwNspdfao2p4FGCS8TTnAbfGYPU3I8CUE",1788965096438]