Classification Models
Source: Concepts adapted from Ace the Data Science Interview — Chapter 7 (“Machine Learning”) by Kevin Huo & Nick Singh. Rewritten in original wording for this course. Coding uses scikit-learn on Piston.
With evaluation tools from Classification fundamentals, this lesson covers the interview workhorses: logistic regression, Naive Bayes, SVMs, and decision trees — when they shine, where they fail, slider explorers (sigmoid, logistic 1-D, entropy, NB posteriors), and short coding drills after each.
Logistic regression
Map a linear score through the sigmoid into a probability:
Default threshold 0.5 → class 1 if , else 0 (tune the threshold for FP/FN costs). Multi-class uses softmax (multinomial logistic).
Why interviews love it: fast, probabilistic, highly interpretable coefficients. Pitfalls: high bias / low variance — struggles with nonlinear boundaries; correlated features muddy (same remedies as linear regression: drop features, regularize).
Training typically minimizes log-loss (cross-entropy).
Logistic regression
1 / 4The sigmoid’s job is to…
Pick an answer to see if you are right.
Compute: sigmoid
1 / 5Sigmoid σ(z)=1/(1+e^(−z)). What is σ(-1)? Round to 2 decimals.
Enter a number (at most 2 decimal places).
Practice: logistic regression
Naive Bayes
Estimate via Bayes’ rule under strong assumptions:
- Features are conditionally independent given
- Features are treated with equal weight in that factorization
Why it works in practice: tiny data needs, very fast, strong text baseline (spam / sentiment) even when independence is false. Caveat: the independence assumption is almost never literally true — yet linear separation often still helps.
Independence cuts the interaction explosion: instead of needing order samples for all feature interactions, you estimate one-dimensional conditionals.
Naive Bayes
1 / 4The “naive” part is mainly…
Pick an answer to see if you are right.
Practice: Naive Bayes
Support vector machines (SVMs)
Find a maximum-margin hyperplane separating classes. Points on the margin boundary are support vectors.
When data are not linearly separable in the original space, a kernel implicitly maps to a higher-dimensional feature space (the kernel trick) so a linear separator there can look nonlinear in input space. Common kernels: linear, RBF / Gaussian.
| Prefer SVM when… | Prefer simpler linear models when… |
|---|---|
| Clear margin / nonlinear boundary, moderate , high | Need probabilities & interpretability, huge |
| Kernel can help separation | Classes heavily overlap / compute budget is tight |
SVMs are weaker when datasets are enormous (cost) or when you must explain a probability to a PM.
SVMs
1 / 4SVMs primarily maximize…
Pick an answer to see if you are right.
Practice: SVM
Decision trees (CART)
A tree recursively splits the feature space to reduce impurity. Each leaf holds a class prediction (or a regression mean — CART covers both).
Training is greedy: at each node, try splits and pick the one with largest information gain.
Entropy of a discrete label :
(Binary: peaks at , zero at .) Trees often use Gini impurity in practice — same “purer leaves are better” idea.
Information gain for a split on :