BeginnerCoder

Lesson 5 — The sigmoid

Turning any number into a probability

A probability is a number from 0 (impossible) to 1 (certain). Models often produce a raw score that can be anything, like −7 or 12. The sigmoid function squashes any score into 0..1: big positive scores go near 1, big negative ones near 0, and 0 becomes exactly 0.5.

import math
1 / (1 + math.exp(-0))   # 0.5

math.exp(x) means e (about 2.718) to the power x.

Why it matters: sigmoid is how a model says "I am 88% sure this email is spam" instead of printing a meaningless score.

Want it another way?
Task

Make sigmoid(x) return the sigmoid of x: 1 divided by (1 + e to the power of minus x). The loop prints the result for -2, 0 and 2.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.