LearnFrenzy
Sign in Take a free test

Core Models · Lesson 4 of 6

Classification Models: Logistic Regression, Decision Trees and Random Forests

Spam or not, fraud or genuine, pass or fail — classification is the most common ML task in business. Learn how the three workhorse classifiers think, their strengths and weaknesses, and how to train and compare them.

13 min read Intermediate Updated Nov 9, 2026 Reviewed by Saurabh Samir

When a bank decides in seconds whether your card payment at 2 am in another city looks suspicious, a classifier is answering one question: which category does this example belong to? Three models handle the majority of real classification work on tabular data. Learn them well and you can solve — and explain — most problems you will meet as a fresher.

1. Logistic regression: a probability from a weighted sum

Despite the name, it is a classifier. Like linear regression, it computes a weighted sum of the features — then squeezes that number into a probability between 0 and 1 with the S-shaped sigmoid function. "Probability this transaction is fraud: 0.83." You then pick a threshold (often 0.5) to turn it into a yes/no decision.

  • Strengths: fast, stable, gives probabilities, coefficients are explainable ("a new device raises the odds of fraud the most").
  • Weaknesses: assumes a roughly linear relationship; needs scaling and feature work for complex patterns.

2. Decision tree: a flowchart learned from data

A tree asks yes/no questions, choosing at each step the question that best separates the classes:

Is the amount > ₹50,000?
├── yes → Is the device new?
│        ├── yes → FRAUD (risk 0.91)
│        └── no  → Is it a usual city? ...
└── no  → GENUINE (risk 0.03)
  • Strengths: easy to explain and draw, handles non-linear patterns and mixed data, no scaling needed.
  • Weaknesses: a deep tree memorises noise (overfitting), and small changes in data can produce a very different tree.

3. Random forest: many trees vote

Train hundreds of trees, each on a random sample of rows and considering a random subset of features at each split, then let them vote. Individual trees make different mistakes; averaging cancels much of the noise. This idea — combining many models — is called an ensemble.

  • Strengths: strong accuracy with little tuning, robust to outliers, gives feature importance.
  • Weaknesses: slower and less interpretable than one tree; larger to store.

Its close cousin, gradient boosting (XGBoost, LightGBM, CatBoost), builds trees one after another, each fixing the previous ones' mistakes. It often wins on tabular data — learn it after you are comfortable with random forests.

Train and compare all three

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, f1_score

X, y = load_breast_cancer(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)

models = {
    "Logistic regression": make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)),
    "Decision tree (depth 4)": DecisionTreeClassifier(max_depth=4, random_state=42),
    "Random forest (300 trees)": RandomForestClassifier(n_estimators=300, random_state=42),
}
for name, model in models.items():
    model.fit(X_train, y_train)
    pred = model.predict(X_test)
    print(f"{name:26s} accuracy={accuracy_score(y_test, pred):.3f}  F1={f1_score(y_test, pred):.3f}")

forest = models["Random forest (300 trees)"]
top = sorted(zip(forest.feature_importances_, X.columns), reverse=True)[:5]
for importance, feature in top:
    print(f"{feature:25s} {importance:.3f}")

Logistic regression is wrapped in a pipeline with scaling because it needs it; the trees do not. Comparing several models on the same split, with the same metrics, is standard practice.

Choosing between them

NeedGood first choice
Explain every decision to a regulator or managerLogistic regression, or a shallow decision tree
Best accuracy on tabular data with little tuningRandom forest, then gradient boosting
Quick baselineLogistic regression
Mixed numeric and categorical data, non-linear patternsTree-based models

Imbalanced classes: the fraud problem

If 1 in 1,000 transactions is fraud, a model that always says "genuine" is 99.9% accurate — and useless. With imbalanced data:

  • Judge models by precision, recall and F1, not accuracy (next lesson).
  • Use stratify=y when splitting so both sets contain the rare class.
  • Try class_weight="balanced" so mistakes on the rare class cost more.
  • Tune the decision threshold instead of accepting 0.5.

Interview tip: when asked "which model would you use?", do not name one immediately. Ask about the data size, the need for explanations and the cost of each type of mistake — then suggest a baseline and a stronger model to compare.

Try it yourself: In the code above, change the decision tree's max_depth to None and compare its train and test accuracy. Then set n_estimators to 10 and 1000 for the forest. Which change matters more?

Common interview questions

Try answering each one aloud before you open it.

1Why is logistic regression called regression if it is used for classification?

It models a continuous value — the log-odds — as a linear combination of features, then converts it to a probability with the sigmoid function. A threshold on that probability gives the class.

2How does a random forest reduce overfitting compared to a single decision tree?

It trains many trees on random samples of the data and random subsets of features, so their errors are less correlated, and averages their votes, which reduces variance.

3How do you handle an imbalanced classification problem?

Use metrics like precision, recall, F1 or PR-AUC instead of accuracy, stratify the split, use class weights or resampling, and tune the decision threshold to the business cost of false positives and false negatives.

Practise this

Build something with it

FAQs

What is the difference between bagging and boosting?

Bagging (random forest) trains models independently on random samples and averages them to reduce variance. Boosting trains models sequentially, each focusing on the previous models’ errors, to reduce bias.

Do I need to scale features for random forests?

No. Trees split on thresholds of individual features, so scaling does not change the result.

References & further reading

Finished this lesson?

Mark it complete. Create a free account to save it on every device.

Next lesson

Search LearnFrenzy

Esc

Quick links