EasyDeepLearn
Supervised Learning · section 3 of 18

Logistic regression & classification basics

29 interview questions on logistic regression & classification basics, each answered in full. Free to read, no account needed.

Precision vs recall — when do you optimize each?

easy
  • Precision = TP / (TP + FP): of the items flagged positive, how many really are.
  • Recall = TP / (TP + FN): of the actual positives, how many we caught.
  • Optimize precision when false positives are costly (spam filter, fraud alert to a user).
  • Optimize recall when missing a positive is costly (cancer screening, security threats).
  • F1 balances them.
#metrics#classificationPermalink & quiz →

How do you read a confusion matrix and derive the key metrics?

easy
  • Rows = actual class, columns = predicted class (or vice versa).
  • Cells: TP, FP, TN, FN.
  • Accuracy = (TP+TN)/all.
  • Precision = TP/(TP+FP).
  • Recall = TP/(TP+FN).
  • Specificity = TN/(TN+FP).
  • F1 = 2·P·R/(P+R).
  • Read it to see which class the model confuses with which — a diagonal-heavy matrix means good separation.
#metrics#classificationPermalink & quiz →

One-vs-Rest, One-vs-One, and softmax — how do you choose for multi-class?

medium
  • One-vs-Rest trains K binary classifiers (class k vs the rest); simple and calibrates per-class, but scores aren't jointly normalized.
  • One-vs-One trains K(K-1)/2 binary models between every pair; more models but each sees a balanced binary problem, useful for SVMs. Softmax / multinomial logistic regression jointly models all classes with a single objective; the natural choice for logistic regression and neural nets and it produces coherent probabilities that sum to one.
#logistic-regression#classification#multiclassPermalink & quiz →

Write out the softmax function and its main property.

easy
  • softmax(z)k  =  exp(zk)  /  sumj\operatorname{softmax}(z)k\; = \;\operatorname{exp}(z_{k})\; / \;\mathrm{sum}_{j} exp(zj)\operatorname{exp}(z_{j}).
  • It maps a vector of real logits to a probability distribution: values in (0, 1) that sum to 1.
  • It's invariant to adding a constant to all logits (that's why we subtract max(z) for numerical stability).
  • The winner is the argmax; adjacent logits produce similar probabilities so the mapping is smooth and differentiable — perfect for gradient descent.
#logistic-regression#classificationPermalink & quiz →

How do you interpret a logistic regression coefficient as an odds ratio?

medium
  • In log(odds)  =  wT\operatorname{log}(\mathrm{odds})\; = \;w^{T} x, a one-unit increase in xjx_{j} multiplies the odds by exp(wj)\operatorname{exp}(w_{j}). exp(wj)\operatorname{exp}(w_{j}) is the odds ratio: 1.5 means a 50% relative increase in odds per unit of xjx_{j}.
  • Positive w    oddsw\; \to \;\mathrm{odds} ratio  >  1    outcome\mathrm{ratio}\; > \;1\; \to \;\mathrm{outcome} more likely; negative w -> < 1    less1\; \to \;\mathrm{less} likely.
  • For a categorical predictor with a reference level, exp(w)\operatorname{exp}(w) is the odds ratio between that category and the reference.
#logistic-regression#interpretabilityPermalink & quiz →

How do class weights work in logistic regression, and when do you use them?

medium
  • Class weights re-weight the log-loss contributions of each class so the minority class carries more penalty when misclassified.
  • 'balanced' in scikit-learn sets weightc  =  nsamples  /  (nclasses    nc)\mathrm{weight}_{c}\; = \;n_{\mathrm{samples}}\; / \;(n_{\mathrm{classes}}\; \cdot \;n_{c}).
  • Effect: pushes the decision boundary toward the majority class and improves recall on the minority class, at the cost of precision.
  • Use when the positive class is rare and false negatives are more costly than false positives.
#logistic-regression#imbalancePermalink & quiz →

'multinomial' vs 'ovr' setting in scikit-learn's LogisticRegression — what changes?

medium
  • 'multinomial' fits a joint softmax model over all K classes with a single log-loss objective — the natural probabilistic model for multi-class.
  • 'ovr' trains K independent binary logistic regressions (class k vs the rest) and normalizes per-example at prediction time.
  • Multinomial usually gives better-calibrated probabilities and slightly better accuracy; OvR is simpler and lets you look at per-class binary models but doesn't guarantee coherent probabilities.
#logistic-regression#multiclass#classificationPermalink & quiz →

Gaussian NB vs Multinomial NB vs Bernoulli NB — when do you use each?

medium
  • Gaussian NB: continuous features assumed to be normally distributed per class (baseline for tabular numeric data).
  • Multinomial NB: counts / term frequencies — the go-to for bag-of-words text classification.
  • Bernoulli NB: binary indicators — text with presence/absence of terms, or clickstream flags.
  • Complement NB is a Multinomial NB variant that's more robust on imbalanced text corpora.
#naive-bayes#text#classificationPermalink & quiz →

When would LDA outperform logistic regression?

hard
  • When (1) classes are approximately Gaussian with similar covariances — LDA is the maximum-likelihood classifier for that model, more efficient than logistic regression which is agnostic to the feature distribution; (2) the training set is small — LDA has fewer parameters and estimates them from all of the data at once; (3) classes are well-separated — logistic regression can suffer from perfect separation, LDA doesn't.
  • Logistic regression usually wins when features are non-Gaussian or highly correlated.
#lda-qda#logistic-regression#classificationPermalink & quiz →

What is the nearest centroid (Rocchio) classifier and when is it useful?

easy
  • Compute the mean feature vector per class; classify a new point as the class whose centroid is closest (usually Euclidean or cosine).
  • Extremely simple, no hyperparameters, very fast, no training beyond averaging.
  • Works well when classes are roughly spherical and well-separated in feature space.
  • Good baseline for text classification with TF-IDF (called Rocchio's method).
  • Fails when class distributions are non-spherical or overlapping.
#knn#classificationPermalink & quiz →

How does temperature scaling calibrate a classifier's probabilities?

medium
  • You take the trained logits z, divide by a scalar T > 0, then softmax: p = softmax(z / T).
  • T is fit on a held-out validation set by minimizing NLL.
  • T > 1 sharpens toward uniform (softens overconfident predictions — the typical case for deep nets); T < 1 sharpens toward one-hot.
  • It changes only confidence, not the argmax, so accuracy is unchanged but calibration (ECE, Brier) improves.
  • Cheap, single-parameter, widely used for post-hoc calibration.
#calibration#classificationPermalink & quiz →

Why is accuracy a bad primary metric for many real-world problems?

easy
  • Accuracy weights every error equally and ignores class balance.
  • On a 99% negative dataset, always predicting negative gives 99% accuracy — a model that's completely useless.
  • Even on balanced problems it hides asymmetric costs (missed fraud is worse than a false alarm, or vice versa).
  • Prefer metrics matched to the deployment cost: precision at fixed recall, F1, PR-AUC, expected cost, Brier score.
  • Always report accuracy alongside the class prior so readers can spot base-rate issues.
#metrics#classification#imbalancePermalink & quiz →

What is F-beta score and when is F1 not enough?

medium
  • Fβ  =  (1  +  β2)    P    R  /  (β2    P  +  R)F_{\beta}\; = \;(1\; + \;\beta^{2})\; \cdot \;P\; \cdot \;R\; / \;(\beta^{2}\; \cdot \;P\; + \;R) generalizes F1 with a weight on recall vs precision. β  =  1\beta\; = \;1 → F1 (equal). β  =  2\beta\; = \;2 (F2) → recall matters 4x more than precision — appropriate for medical screening where missing positives is very costly. β  =  0.5\beta\; = \;0.5 (F0.5) → precision matters 4x more — for a spam filter where false positives annoy users.
  • F1's implicit assumption is that precision and recall are equally important — often they aren't.
#metrics#classificationPermalink & quiz →

Macro vs micro vs weighted averaging in multi-class metrics — how do you choose?

medium
  • Macro: compute metric per class, then average with equal weight.
  • Treats each class equally — useful when small classes matter (e.g., rare diseases).
  • Micro: aggregate TP/FP/FN across all classes then compute the metric globally.
  • Dominated by frequent classes; equals accuracy for single-label problems.
  • Weighted: per-class metric weighted by class support.
  • Compromise between macro and micro.
  • Rule of thumb: report macro when classes have unequal importance; micro/weighted when the aggregate is the target.
#metrics#classification#multiclassPermalink & quiz →

What is Cohen's kappa and why is it useful?

medium
  • Cohen's κ  =  (po    pe)  /  (1    pe)\kappa\; = \;(p_{o}\; - \;p_{e})\; / \;(1\; - \;p_{e}), where pop_{o} is observed agreement (accuracy) and pep_{e} is the agreement expected by chance given class marginals.
  • It corrects accuracy for the baseline of random guessing given the class prior.
  • Range: [-1, 1]. 0 = no better than chance; 1 = perfect.
  • Rules of thumb: 0.2 slight, 0.4 fair, 0.6 substantial, 0.8+ almost perfect.
  • Useful for imbalanced classification and inter-annotator agreement.
#metrics#classification#imbalancePermalink & quiz →

What is the Matthews Correlation Coefficient (MCC) and when should you use it?

medium
  • MCC = (TP*TN - FP*FN) / sqrt((TP+FP)(TP+FN)(TN+FP)(TN+FN)) — the Pearson correlation coefficient between predicted and true binary labels.
  • Range [-1, 1]: 1 perfect, 0 chance, -1 perfectly wrong.
  • Its advantage over F1: symmetric in positive and negative classes (F1 ignores TN), so it's a fair single-number summary even on imbalanced data.
  • Great for reporting binary classifier quality in one number.
#metrics#classification#imbalancePermalink & quiz →

What is log loss (cross-entropy) and what does a specific value mean?

medium
  • For binary: log loss = -1/n * sum [y log(p) + (1-y) log(1-p)].
  • It's the negative log-likelihood of the observed labels under the predicted probabilities — proper scoring rule that rewards well-calibrated confident predictions and heavily punishes confident mistakes. log(0.5)\operatorname{log}(0.5) ≈ 0.693 is the loss of always predicting 0.5 (max-uncertainty baseline).
  • Above that, your model is worse than uninformative; below, it's actually learning something.
  • Never use log loss for evaluation on a mis-calibrated model.
#metrics#classification#calibrationPermalink & quiz →

What is the Brier score and how does it compare to log loss?

medium
  • Brier = 1/n * sum (pi    yi)2(p_{i}\; - \;y_{i})2 — mean squared error between predicted probability and true label.
  • Range [0, 1] for binary.
  • Like log loss, it's a proper scoring rule that penalizes miscalibration.
  • Differences: Brier is bounded and quadratic (less punishing on confident mistakes than log loss), decomposable into calibration + refinement + uncertainty terms (Murphy decomposition), and interpretable in probability units.
  • Prefer Brier when confident misprediction shouldn't dominate the metric.
#metrics#classification#calibrationPermalink & quiz →

What is top-k accuracy and when do you use it?

easy
  • Top-k counts a prediction as correct if the true class is among the top-k predicted classes ranked by score.
  • Common in image classification with many classes (ImageNet top-5), recommender systems (was the right item in the top-10 recommendations?), and retrieval-style tasks.
  • Top-k relaxes the single-choice requirement — appropriate when downstream UX shows multiple candidates.
  • Report both top-1 and top-k for calibration between raw and forgiving evaluation.
#metrics#classification#multiclassPermalink & quiz →

What is balanced accuracy?

easy
  • Balanced accuracy = (sensitivity + specificity) / 2 for binary, or the macro average of per-class recall for multi-class.
  • It corrects accuracy by treating every class equally regardless of size — a 50% baseline for random guessing on any class balance.
  • Handy single-number summary for imbalanced classification; equivalent to accuracy on perfectly balanced data.
  • Doesn't use precision, so pair it with PR-AUC when the positive class is what you care about.
#metrics#classification#imbalancePermalink & quiz →

What is the geometric mean (G-mean) in classification metrics?

medium
  • G-mean = sqrt(sensitivity * specificity).
  • Only high when both true positive and true negative rates are high — a single point going to zero drags the metric to zero.
  • Useful for imbalanced problems where you want a classifier that performs well on both classes, not one that trivially favors the majority.
  • Common in medical diagnostics.
#metrics#classification#imbalancePermalink & quiz →

What is Youden's J statistic and how is it used?

medium
  • J = sensitivity + specificity - 1.
  • It's the vertical distance from the ROC curve to the diagonal at a given operating point.
  • The threshold that maximizes J is the 'Youden optimal' operating point — often used in medical statistics to pick a screening cutoff that balances sensitivity and specificity.
  • Assumes equal cost of FP and FN — use cost-weighted variants when they differ.
#metrics#classification#calibrationPermalink & quiz →

How do you interpret an ROC curve, and what point matters?

medium
  • ROC plots True Positive Rate (sensitivity) vs False Positive Rate (1 - specificity) as the decision threshold sweeps from 1 to 0.
  • The diagonal is random guessing; the top-left corner is perfect.
  • AUC is the area under it — the probability that a random positive is scored higher than a random negative.
  • The 'best' operating point depends on cost: often top-left-most, or the intersection with an iso-cost line matching your FP/FN penalties.
#metrics#classificationPermalink & quiz →

What does ROC-AUC really measure, and what's a good value?

easy
  • AUC is the probability that a random positive example is scored higher than a random negative example (equivalent to the Mann-Whitney U statistic normalized). 0.5 = random; 1.0 = perfect ranking.
  • Rules of thumb: 0.7 acceptable, 0.8 good, 0.9+ excellent.
  • Beware: an AUC in isolation says nothing about calibration or the actual decision threshold — a classifier can have high AUC and still make horrible probability estimates.
#metrics#classificationPermalink & quiz →

What are the main pitfalls of relying on ROC-AUC?

medium
  • (1) On heavily imbalanced data, ROC-AUC is optimistic because the false positive rate is dominated by a huge number of negatives — PR-AUC is more informative.
  • (2) AUC is threshold-independent — a model with great AUC can still have terrible precision at any usable threshold.
  • (3) It doesn't reward calibration — two models with identical AUC can have very different probability quality.
  • (4) Comparing AUC across datasets with different class balances is misleading.
  • Always pair AUC with a calibration metric and a cost-aware operating point.
#metrics#classification#imbalancePermalink & quiz →

How do you select the decision threshold for a binary classifier?

medium
  • Compute predicted probabilities on validation, then choose the threshold based on your business objective: (a) maximize F1 → argmax over t of F1(y, p > t); (b) fixed recall → smallest t with recall ≥ target, maximizing precision; (c) expected cost → t that minimizes FPcostFP  +  FNcostFN\mathrm{FP}_{\mathrm{cost}} \cdot \mathrm{FP}\; + \;\mathrm{FN}_{\mathrm{cost}} \cdot \mathrm{FN}.
  • Never tune threshold on the test set.
  • If probabilities are poorly calibrated, calibrate first, then threshold.
  • Threshold tuning is often more impactful than model choice.
#metrics#classification#calibration#imbalancePermalink & quiz →

How do you evaluate a multi-label classifier?

medium
  • Common metrics: (a) Hamming loss = fraction of misclassified labels (per-label error) — lenient; (b) exact match / subset accuracy = fraction of examples where ALL labels are correctly predicted — strict; (c) per-label F1 with macro/micro averaging — the workhorse; (d) label ranking metrics (mean average precision, coverage error) when scores matter more than binary decisions.
  • Always report per-label metrics too, especially when label frequencies differ.
#metrics#classification#multilabelPermalink & quiz →

MAP, NDCG, MRR — when do you use each in ranking?

hard
  • MRR (Mean Reciprocal Rank): 1/rank of the first relevant item, averaged over queries.
  • Ideal for 'find one right answer' tasks.
  • MAP (Mean Average Precision): mean of per-query average precision — for tasks with multiple relevant items and only binary relevance.
  • NDCG (Normalized Discounted Cumulative Gain): uses graded relevance and log-position discount — the standard for search / recommendation ranking with graded labels.
  • Use MRR for factoid QA, MAP for binary relevance, NDCG for graded relevance.
#metrics#classificationPermalink & quiz →

How do you evaluate a classifier when FP and FN have different costs?

hard
  • Assign explicit costs CFPC_{\mathrm{FP}} and CFNC_{\mathrm{FN}}.
  • Compute expected cost per example  =  CFP    FPR    P(neg)  +  CFN    FNR    P(pos)\mathrm{example}\; = \;C_{\mathrm{FP}}\; \cdot \;\mathrm{FPR}\; \cdot \;P(\mathrm{neg})\; + \;C_{\mathrm{FN}}\; \cdot \;\mathrm{FNR}\; \cdot \;P(\mathrm{pos}).
  • Pick the threshold that minimizes it on validation.
  • Report expected loss in dollar / operational units, not just precision-recall.
  • If costs are unknown but ordinal (FN worse than FP), use ROC iso-cost lines: parallel lines whose slope encodes the FP/FN cost ratio — pick the operating point where the line first touches the ROC curve.
#metrics#classificationPermalink & quiz →

Practise Supervised Learning