Stop 09 · Advanced Analytics & Predictive BasicsTopics · 3/8

Logistic Regression Basics

Part of the Data Analyst Roadmap

In short

Will this customer churn? Will this loan default? Will this lead convert? Yes-or-no questions are everywhere, and logistic regression is the simple, explainable first model for them.

A close cousin of linear regression used for classification problems — predicting a yes/no outcome (e.g. will this customer churn) rather than a continuous number.

The jaadu studies with books and a clock, holding thumbs-up and thumbs-down cards.

Think of it like this

Pass or fail, from study hours

Your senior says: study 1 hour, you'll probably fail; 6 hours, you'll almost surely pass. In between, it's a chance. Logistic regression turns hours of study into a probability of passing, an S-shaped curve that never goes below 0 or above 1.

Watch it happen

Chance of passing by study hours (illustrative)

0.05
0
0.12
1
0.3
2
0.55
3
0.78
4
0.92
5

1/5Each cell is 1 to 6 hours of study. The model outputs a probability of passing for each.

The big ideas

What you need to know

Outputs a probability

Between 0 and 1. You choose the cut-off that turns it into yes or no.

Threshold is a business choice

Flagging fraud? Use a lower cut-off to catch more, accepting more false alarms.

Confusion matrix

Counts true and false positives and negatives. Accuracy alone hides what kind of mistakes you make.

Watch class imbalance

If 2% of customers churn, predicting 'nobody churns' is 98% accurate and useless.

See it in code

Walk through the code

python
1from sklearn.linear_model import LogisticRegression2from sklearn.model_selection import train_test_split3from sklearn.metrics import confusion_matrix, classification_report4 5X = df[["orders_last_90d", "days_since_last_order", "complaints"]]6y = df["churned"]7X_train, X_test, y_train, y_test = train_test_split(8    X, y, test_size=0.25, stratify=y, random_state=42)9 10model = LogisticRegression(max_iter=1000).fit(X_train, y_train)11proba = model.predict_proba(X_test)[:, 1]12pred = (proba >= 0.4).astype(int)13 14print(confusion_matrix(y_test, pred))15print(classification_report(y_test, pred))

1/4Three inputs and a 0/1 outcome column.

A churn model with a held-out test set and a chosen threshold of 0.4. Column names are examples.

Save yourself the pain

Common mistakes

Oops

Celebrating 97% accuracy on a dataset where 97% are 'no'.

Do this instead

Check the confusion matrix, precision and recall for the 'yes' class.

Oops

Using the default 0.5 threshold without thinking.

Do this instead

Pick the threshold from the cost of each mistake, with the business team.

Your mission

Your first classifier

~35 min

Quick check

Only 3% of transactions are fraud. A model predicts 'not fraud' for everything. What's its accuracy, and is it useful?

Go deeper

  1. 1Learn conceptually how logistic regression outputs a probability, not a raw number.
  2. 2Fit a basic logistic regression model on a simple binary-outcome dataset.
  3. 3Understand the difference between classification accuracy and a confusion matrix.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge