# What Is Logistic Regression?

> How logistic regression turns a few facts into a probability, where banks use it, and how to explain it, for your AI interview.

**In short:** Logistic regression is a classic model that predicts the probability of a yes or no outcome, such as whether a card payment is fraud. It adds up weighted facts, then a sigmoid function squeezes the result between 0 and 1, so the output reads as a chance.

AI and LLM interview question Q432, Classic ML and deep-learning basics. Beginner, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-logistic-regression

## Say it in 30 seconds

Logistic regression is a simple, classic model that gives a probability for a yes or no question. Take a card payment: the model weighs the amount, the device and the country, adds them up, and squeezes the result into a number between 0 and 1. That number is the chance of fraud. Despite its name, it is a classifier: it sorts payments into groups. It is a good first model to try, because it is an extremely efficient mechanism for calculating probabilities.

## Say it in 2 minutes

Logistic regression is a model for yes or no questions. Its output is a probability, a number from 0 to 1. Take a bank that wants a score for each card payment: how likely is this one to be fraud? The model starts from a few facts about the payment, such as whether the amount is far above the customer's usual spend, whether the phone is new to the account, and whether the card is being used abroad. Each fact gets a weight that says how much it matters.

The model adds up the weighted facts into one number called the log-odds. That number can be any size, positive or negative. A sigmoid function then squeezes it into a value between 0 and 1, and that value is read as a probability. A big positive score gives a chance close to 1, and a big negative score gives a chance close to 0.

The model is used for classification, which means sorting items into groups. The bank picks a threshold, which defaults to 0.5 in scikit-learn, to decide between approve and hold. The threshold is a business choice: lower it and you catch more fraud, but you block more genuine customers.

In the interview, say three things. It outputs a probability. Its weights are learned from labelled history. It trains with log loss, which punishes confident wrong guesses hard. Then add the honest limit: it is a linear model, so it only works well when the facts you feed it are the right ones.

## What is logistic regression, in plain words?

Logistic regression is a model that answers a yes or no question with a chance, such as the chance that a card payment is fraud. Despite its name, in scikit-learn and ML terms it is a linear model for classification, which means sorting things into groups.

- **Linear regression: Predicts a number.** For example, the delivery time of an order in days. The answer can be any number.
- **Logistic regression: Predicts a chance.** For example, the chance that a card payment is fraud. The answer always sits between 0 and 1.

Think of a fraud clerk at a bank. She looks at a card payment and weighs a few facts: is the amount far above the customer's usual spend, is the phone new to the account, is the card being used abroad? Each fact nudges her gut feeling up or down a little. Logistic regression does the same weighing with numbers, then turns the total into one chance. A bank's scoring system runs that sum for every card payment, using weights learned from past payments that were already marked fraud or genuine.

- **Probability:** A number from 0 to 1. 0.93 means a 93% chance.
- **Sigmoid function:** An S-shaped curve that squeezes any number into a value between 0 and 1.
- **Log-odds:** The raw score the model adds up before the squeeze. Also called z.
- **Threshold:** The cut-off that turns a probability into a decision, 0.5 by default in scikit-learn.

## How does it turn signals into a chance?

Every input goes through three steps.

1. **Weigh each signal:** Each fact is multiplied by its weight. A bigger weight moves the answer more.
2. **Add them up:** The weighted facts and a starting number called the bias make one score, the log-odds.
3. **Squeeze to a chance:** The sigmoid function turns the score into a value from 0 to 1, which we read as a probability.

*Diagram, How a card payment gets a chance of fraud: Each fact about a payment carries a weight. The model adds them into one score, and the sigmoid turns that score into a chance. The threshold then decides whether the payment is approved or held. The weights in this picture are made up.*
1. The amount signal goes in, multiplied by its weight.
2. The new device signal goes in, multiplied by its weight.
3. The card-abroad signal goes in, multiplied by its weight.
4. The weighted signals and the bias make one score, z.
5. The sigmoid turns z into a chance. A low chance is approved.
6. A high chance, at or above the threshold, is held for review.

The sigmoid curve is what makes the output a probability. Very negative scores give a chance near 0, and very positive scores give a chance near 1. A few readings make it concrete:

- **Score 0:** A 50% chance.
- **Score 2:** About 88%.
- **Score -2:** About 12%.
- **Any score:** Never exactly 0% or 100%, however large or small the score.

> **Tip:** Say that the output is a probability. The yes or no comes later, from a threshold you choose.

With more than two classes, scikit-learn uses the softmax function instead. It gives one probability for each class.

## How is it trained, and why not squared loss?

A model learns its weights from past examples whose answers are already known, such as payments marked fraud or genuine. For each example it guesses a chance, measures how wrong the guess was, and moves the weights to do better next time.

*Diagram, How a logistic model learns its weights: The model learns from past payments whose answers are known. Each guess is scored with log loss, and the weights move to shrink the error. Over many examples, the weights settle into a model that can score new payments.*
1. Past payments, with the true answer, teach the model.
2. The model guesses a chance for each one.
3. Log loss measures how far off each guess was.
4. After many small steps the weights settle, and the model is ready.

Linear regression usually measures error with squared loss. Logistic regression does not suit it well, because the sigmoid curve is s-shaped rather than linear, so the same change in the score moves the chance by different amounts. Log loss fits that curve better. It punishes a confident wrong guess hard: saying 2% fraud for a payment that was fraud costs far more than saying 40%.

> "Logistic regression models use Log Loss as the loss function instead of squared loss." (Google Machine Learning Crash Course)

Training also needs regularization, a penalty that stops the weights growing too large. Without it, the model can memorise its history and miss new payments. This is overfitting. Google's crash course calls regularization critical here, and scikit-learn applies it by default.

## Can you run the fraud example by hand?

Here are three card payments. The weights are made up for this sketch: amount far above usual 2.2, new device 1.8, card abroad 1.5, and a bias of -4. A real bank learns its weights from past data.

A usual grocery payment has no warning signs, so the score is -4 and the chance is 1.8%. A new phone with the card abroad gives a score of -0.7 and a 33.2% chance. That is below a 0.5 threshold, so it is approved. A large payment from a new phone, abroad, gives a score of 1.5 and an 81.8% chance, so it goes to review.

```python
import math

# Made-up weights for a card-payment fraud score. A real model learns them from past payments.
WEIGHTS = {"amount_vs_usual": 2.2, "new_device": 1.8, "card_abroad": 1.5}
BIAS = -4.0

def fraud_probability(signals):
    # z is the log-odds: each signal times its weight, added up, plus the bias.
    z = BIAS + sum(WEIGHTS[name] * value for name, value in signals.items())
    # The sigmoid turns z into a probability between 0 and 1.
    return 1 / (1 + math.exp(-z))

payments = {
    "Usual grocery payment": {"amount_vs_usual": 0, "new_device": 0, "card_abroad": 0},
    "New phone, card abroad": {"amount_vs_usual": 0, "new_device": 1, "card_abroad": 1},
    "Large, new phone, abroad": {"amount_vs_usual": 1, "new_device": 1, "card_abroad": 1},
}

for name, signals in payments.items():
    p = fraud_probability(signals)
    action = "hold for review" if p >= 0.5 else "approve"
    print(f"{name}: {p:.1%} fraud chance, {action}")
```

Output:

```text
Usual grocery payment: 1.8% fraud chance, approve
New phone, card abroad: 33.2% fraud chance, approve
Large, new phone, abroad: 81.8% fraud chance, hold for review
```

The code is only a sketch. It shows the three steps, and the last check decides the action.

### How would you pick the threshold for a fraud model?

The 0.5 cut-off is only the default. The threshold is a business choice, because each mistake costs something different. Missed fraud loses money. A blocked genuine payment annoys a customer, who may leave.

- **Lower, say 0.3: More fraud caught.** But more genuine payments are blocked, and the call centre gets more complaints.
- **Higher, say 0.8: Fewer false alarms.** But more fraud gets through, and the bank pays for it.

A sensible way to choose: test a few thresholds on past payments, count the missed fraud and the blocked genuine payments at each one, and agree the balance with the risk team. Check it again when fraud patterns change.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a model that gives the probability of a yes or no outcome.
- [ ] The sigmoid: it squeezes a score into a value between 0 and 1.
- [ ] Why it is called regression but used for classification.
- [ ] Log loss for training, and regularization against overfitting.
- [ ] The threshold is a choice that depends on the cost of each kind of mistake.
- [ ] A real case, such as fraud or loan default, and why it is a good first model.

**Common traps**

- Trap: "It predicts a yes or no answer." Better: It predicts a probability. A threshold turns that probability into yes or no.
- Trap: "Its name says regression, so it predicts numbers." Better: It is a classifier. Its output is a chance between 0 and 1, such as 0.93 for a likely fraud.
- Trap: "Use 0.5 as the cut-off, it is always right." Better: 0.5 is only the default. Pick the threshold from the cost of missed fraud against blocked payments.

## Can you spot the mistake?

**Spot the mistake:** A junior analyst wrote four notes about a fraud model. Which note is wrong?

A. The model gives a probability between 0 and 1 for each payment.
B. The sigmoid keeps every output between 0 and 1, so it reads as a chance.
C. Its output can be any number, like a price of 412.50, so the bank should add the outputs up.
D. A threshold of 0.5 is the default, and the bank can change it.

**Answer:** C. A price is what linear regression predicts. Logistic regression stays between 0 and 1, so it can never output 412.50. Adding chances up is not a sensible fraud rate either, so the note is wrong on both counts.

**Quick check:** A fraud model gives 0.4 to a payment that turns out to be fraud. The threshold is 0.5. What happens?

A. It is held for review, because 0.4 is above 0.5.
B. It is approved, because 0.4 is below 0.5, so the fraud gets through.
C. The model is broken, because chances must be above 0.5.
D. It is blocked, because fraud always scores high.

**Answer:** B. Anything below the threshold is approved, so this fraud slips past. A lower threshold, such as 0.3, would have held it, at the cost of blocking more genuine payments.

## Can you answer these from memory?

**Flash cards**

- **What is logistic regression?** A classic model that gives the probability of a yes or no outcome.
- **Does it predict numbers?** No. It predicts a chance between 0 and 1. Linear regression predicts continuous numerical values.
- **What does the sigmoid do?** It squeezes any score into a value between 0 and 1.
- **Why is it called regression?** In scikit-learn and ML terms, it is a linear model for classification, despite its name.
- **Which loss does it train with?** Log loss, not squared loss, plus regularization against overfitting.
- **What is the threshold?** The cut-off, 0.5 by default in scikit-learn, that turns a chance into a decision.

## Likely follow-up questions

- Q430 What is a confusion matrix? https://designervenkat.online/interview/ai/what-is-a-confusion-matrix
- Q437 Softmax vs sigmoid? https://designervenkat.online/interview/ai/softmax-vs-sigmoid
- Q429 What is early stopping? https://designervenkat.online/interview/ai/what-is-early-stopping
- Q436 How do you tune hyperparameters? https://designervenkat.online/interview/ai/how-do-you-tune-hyperparameters
- Q412 Bias vs variance? https://designervenkat.online/interview/ai/bias-vs-variance

## Learn it in depth

- [Logistic regression: yes or no](https://designervenkat.online/learn/machine-learning/logistic-regression)
