What Took Six Labs, Scikit-Learn Does in Three Lines
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 already exists ready-made in a library, exactly like we already saw on the linear regression side.
The entire code
from sklearn.linear_model import LogisticRegression
lr_model = LogisticRegression()
lr_model.fit(X, y)
LogisticRegression() creates the model, .fit(X, y) trains it. No writing sigmoid, no writing compute_gradient_logistic, no writing gradient_descent. Under the hood, sklearn does all the work the last six labs broke down line by line.
y_pred = lr_model.predict(X)
print("Prediction on training set:", y_pred)
Output:
Prediction on training set: [0 0 0 1 1 1]
print("Accuracy on training set:", lr_model.score(X, y))
Output:
Accuracy on training set: 1.0
On the usual six-patient dataset, it gets everyone right. Not surprising, the hand-trained boundary from the last lab already did too.
The detail sklearn is hiding (for now)
Peek at the weights sklearn found:
print("coefficients:", lr_model.coef_, "intercept:", lr_model.intercept_)
Output:
coefficients: [[0.9041 0.7359]] intercept: [-2.3337]
Notice these numbers don't match the 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 LogisticRegression(), 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 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.
Wrapping up
| What I already knew | What this lecture settled |
|---|---|
| I built sigmoid, cost and gradient descent from scratch | sklearn packages all of it into fit/predict/score, same as linear regression already showed |
| The weights I trained by hand solve the problem with no penalty at all | sklearn's weights differ by default, because it already regularizes without me asking |
Practical Application
Same real dataset from the last lab (worst concave points, 60-patient sample, z-score normalized):
lr_model = LogisticRegression()
lr_model.fit(X_train_z, y_train)
print("coefficient:", lr_model.coef_, "intercept:", lr_model.intercept_)
print("accuracy:", lr_model.score(X_train_z, y_train))
Output:
coefficient: [[2.4562]] intercept: [-0.8278]·accuracy: 0.9
Same accuracy (0.90) plain gradient descent found on this exact dataset, but sklearn's coefficient (2.4562) is a lot smaller than the w≈4.175 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.