Classification: Predicting a Category
When the answer is a category — pass/fail, spam/not-spam, which of 3 products — that's classification. The workhorse starting model is logistic regression (a classifier, despite the name).
Same two verbs, different output
fit and predict work exactly as before. The difference: predict returns a class (0 or 1) instead of a number. And you get a bonus method:
predict_proba(X)— the model's confidence for each class.0.92means "92% sure this student passes." Probabilities let you set your own threshold instead of blindly trusting the default 0.5 — critical when a false negative costs more than a false positive.
Accuracy — and its trap
accuracy_score = fraction predicted correctly. Simple, but misleading on imbalanced data: if 95% of emails are "not spam," a model that always says "not spam" scores 95% while catching zero spam. When classes are lopsided, accuracy alone lies — you'll need precision/recall (next lessons) to see the truth.
Try it
- Print
clf.predict_probafor a[[1, 3]]student. Is the model confident they fail? - Flip one label in
yand re-fit. How much does test accuracy move? Small datasets are fragile.
3 graded exercises
Write the code, click Check. We’ll tell you exactly what to fix.
- Exercise 1
Write
fit_classifier(X, y)returning a fittedLogisticRegression. - Exercise 2
Write
pass_probability(model, X_row)returning the probability of class1for one rowX_row(a list of features). - Exercise 3
Write
test_accuracy(X, y)that splits (30% test,random_state=0), fits logistic regression on train, and returns test accuracy.