Predicts a probability by squashing a linear combination of features through the sigmoid function: σ(z) = 1 / (1 + e⁻ᶻ), mapping any real number to (0,1).
Sigmoid curve — the decision boundary is simply where predicted probability crosses the threshold (default 0.5).
The decision boundary itself is linear in the feature space (a straight line/plane) — logistic regression just wraps that linear boundary in a probability interpretation.
Classifies a new point by majority vote among its k closest training points (by distance, usually Euclidean).
Very flexible, jagged decision boundary — low bias, high variance. Sensitive to noise; a single mislabeled neighbor can flip a prediction.
Smoother boundary, averages over more neighbors — higher bias, lower variance. Too large and it starts ignoring local structure, underfitting toward the majority class.
Splits data recursively on the feature/threshold that best separates classes, forming a tree of if/else rules.
Measures how often a randomly picked element would be misclassified if labeled randomly according to the node's class distribution. Faster to compute — the default in most libraries (e.g. sklearn's CART).
Measures disorder/uncertainty in a node; splits chosen to maximize information gain. Slightly more computationally expensive (uses logs); in practice gives very similar trees to Gini.
Applies Bayes' theorem to compute P(class | features), assuming every feature is conditionally independent given the class — the "naive" assumption.
Finds the hyperplane that separates classes with the maximum margin — the widest possible gap between the boundary and the nearest points of each class (the "support vectors").
When classes aren't linearly separable in the original space, the kernel trick implicitly maps points into a higher-dimensional space where a straight-line separator exists — without ever explicitly computing that transformation. Common kernels: linear, polynomial, RBF (Gaussian).
| Metric | Formula (intuition) | Answers |
|---|---|---|
| Precision | TP / (TP + FP) | Of everything I flagged positive, how much was actually positive? |
| Recall | TP / (TP + FN) | Of all actual positives, how many did I catch? |
| F1 | Harmonic mean of precision & recall | A single number balancing both — punishes a big gap between them. |
Plots True Positive Rate vs False Positive Rate across thresholds. Good default for roughly balanced classes.
Plots Precision vs Recall across thresholds. Preferred for imbalanced data — ROC-AUC can look deceptively high when negatives vastly outnumber positives, since FPR stays low even with many false positives in absolute terms.
The default 0.5 cutoff is arbitrary — pick a threshold based on the real cost of false positives vs false negatives. E.g. in cancer screening, missing a real case (FN) is far worse than a false alarm (FP), so lower the threshold to boost recall even at the cost of precision.
from sklearn.linear_model import LogisticRegression from sklearn.metrics import classification_report from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, stratify=y, random_state=42) model = LogisticRegression() model.fit(X_train, y_train) y_pred = model.predict(X_test) print(classification_report(y_test, y_pred)) # prints precision, recall, f1-score, support per class
| Scenario | Good fit | Why |
|---|---|---|
| Need interpretable, explainable output for stakeholders | Logistic Regression, Decision Tree | Coefficients / if-else rules are easy to explain in plain language. |
| Small dataset, few features | KNN, Naive Bayes, Logistic Regression | Don't need huge data to estimate few/simple parameters. |
| Text classification / spam / sentiment | Naive Bayes, Logistic Regression | Handle high-dimensional sparse features well and train fast. |
| Non-linear decision boundary, structured/tabular data | Decision Tree, SVM (RBF kernel), or ensembles | Can capture curves/interactions a linear model would miss. |
| High-dimensional data with clear margin between classes | SVM | Margin maximization tends to generalize well when a real gap exists. |
| Need probability estimates, not just labels | Logistic Regression, Naive Bayes | Naturally output calibrated-ish probabilities (logistic regression more so). |
| Very large dataset, need fast inference | Logistic Regression, Naive Bayes | KNN and SVM both get slow to predict on at scale. |
| Imbalanced classes, cost of false negatives is high | Any model + threshold tuning / class weights | The algorithm matters less here than tuning the threshold and using PR-AUC to evaluate. |
| Baseline / quick benchmark before trying complex models | Logistic Regression | Fast, well-understood, gives a sanity-check number to beat. |