Rana Maninder Singh
Lesson 4 of 4Math for ML (bridge)

eˣ, log & loss

Lesson opener

In one line: \(e^{x}\) stays positive, sigmoid \(1/(1+e^{-z})\) turns C0001's score 1.8717 into probability 0.8667, and log turns a product of likelihoods into a sum you can add.

Why it matters: A churn model cannot ship an unbounded score to a call list. It needs a number between 0 and 1. That squash is \(e^{-z}\). The training loss that respects probabilities is full of logs. This lesson is those two tools, on the same customer vector as math2.

Before this lesson: Loss & gradient descent. Pins: numpy==2.1.3, pandas==2.3.3.

Practitioner focus: Weights stay [0.02, 0.01, 0.5]. C0001's \(z\) is 1.8717. Sigmoid of that is 0.8667 — a high score, not a calibrated 87% from a fitted model.

What you will learn

By the end of this lesson you will be able to:

  1. Print np.exp at −2, −1, 0, 1, 2 and see it never goes negative.
  2. Compute sigmoid(0) = 0.5 by hand and in code.
  3. Squash C0001's real \(z = 1.8717\) to 0.8667.
  4. Compare C0004's higher \(z\) (3.8212) → a probability closer to 1.
  5. Use np.log on 0.5 and 1.0 and say why log loss likes logs.
  6. Refuse to read 0.8667 as "86.67% sure to churn" without a calibrated model.

Worked example: from score to (0, 1)

Step 1 — \(e^{x}\) is always positive.

import numpy as np

xs = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
print(np.round(np.exp(xs), 3))

Expected output:

[0.135 0.368 1.    2.718 7.389]

At 0 the result is 1. Negative inputs shrink toward 0 but never cross it.

Step 2 — Sigmoid at 0 is a coin flip.

import numpy as np

def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))

print(round(float(sigmoid(0.0)), 4))
print(round(float(sigmoid(-4.0)), 4))
print(round(float(sigmoid(4.0)), 4))

Expected output:

0.5
0.018
0.982

Step 3 — C0001's score from math2, now a probability.

import pandas as pd
import numpy as np

def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))

df = pd.read_csv("data/customers.csv")
cols = ["tenure_months", "monthly_spend", "support_tickets"]
x = df.iloc[0][cols].to_numpy(dtype=float)
w = np.array([0.02, 0.01, 0.5])
z = float(x @ w)
print(round(z, 4))
print(round(float(sigmoid(z)), 4))

Expected output:

1.8717
0.8667

Step 4 — C0004 had the high score 3.8212 because of six tickets.

import pandas as pd
import numpy as np

def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))

df = pd.read_csv("data/customers.csv")
cols = ["tenure_months", "monthly_spend", "support_tickets"]
x = df.loc[df["customer_id"] == "C0004", cols].iloc[0].to_numpy(dtype=float)
w = np.array([0.02, 0.01, 0.5])
z = float(x @ w)
print(round(z, 4))
print(round(float(sigmoid(z)), 4))

Expected output:

3.8212
0.9786

Higher \(z\), closer to 1. Still a teaching weight, not a production risk score.

Step 5 — Log: products become sums. Binary log loss uses \(\log(\hat{p})\) and \(\log(1-\hat{p})\). log(1) = 0 (perfect yes). log(0.5) is about −0.693.

import numpy as np

print(round(float(np.log(1.0)), 4))
print(round(float(np.log(0.5)), 4))

Expected output:

0.0
-0.6931

A model that says 0.5 when the answer was yes is charged 0.693 nats. A model that says 1.0 is charged 0. That is why logs appear in the loss — not because someone likes the symbol.

Pause and think

Q1. Why not just clip \(z\) to [0, 1]?

A hard clip has a flat derivative at the edges. Sigmoid stays smooth so a training step still has a direction.

Q2. Is 0.8667 C0001's churn probability?

No. These weights were not fitted to churned. It is "what sigmoid does to this score."

Q3. What is \(e^{0}\)?

1. That is why sigmoid(0) = 1/(1+1) = 0.5.

Guided practice: sigmoid of the 300-row mean score

The mean \(z\) from math2 was 1.7264. Print sigmoid of that mean.

import pandas as pd
import numpy as np

def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))

df = pd.read_csv("data/customers.csv")
cols = ["tenure_months", "monthly_spend", "support_tickets"]
X = df[cols].to_numpy(dtype=float)
w = np.array([0.02, 0.01, 0.5])
z_mean = float((X @ w).mean())
print(round(z_mean, 4))
print(round(float(sigmoid(z_mean)), 4))

Expected output:

1.7264
0.849

Independent practice: exp(1)

Print e to three decimals — np.exp(1).

import numpy as np

print(round(float(np.exp(1.0)), 3))

Expected output:

2.718

Common mistake

The deliberate mistake

Symptom: someone emails "C0001 is 86.67% likely to churn" from this lesson's 0.8667.

Diagnosis: sigmoid of a toy score is not a calibrated probability from a fitted model.

Correction: fit on labelled churned (as in BA 2), then talk about precision and recall.

Prevention: say "score after squash" until a held-out table exists.

Where this connects

What you can now do:

  • Read \(e^{x}\) and sigmoid as "stay positive" and "squash to (0, 1)."
  • Put C0001's 1.8717 through that squash and get 0.8667.
  • Know why log loss uses logs.

Next lesson: What is machine learning?. The symbols are in place. Next is what "learning from labelled rows" means.