MyMisPal
← Practical AI lessons
Lesson 5 of 11

Classification: who is about to leave?

Losing a loyal customer costs more than winning a new one. Nakhla wants to know which store will stop ordering (churn) before it happens, so sales can reach out. We have 10 stores whose outcome we know, and one new store to predict.

The idea behind k-nearest neighbours is simple: “tell me who you resemble and I'll tell you what you'll do”. Measure the distance from the new store to every known store, take the k closest (say 3), and see what happened to most of them.

distance = √((orders₁ − orders₂)² + (weeks₁ − weeks₂)²)
PYTHON
# [orders per month, weeks since last order, outcome]
stores = [
    [8, 1, "stays"], [7, 2, "stays"], [9, 1, "stays"], [6, 3, "stays"],
    [2, 7, "leaves"], [1, 9, "leaves"], [3, 6, "leaves"], [2, 8, "leaves"],
    [6, 4, "stays"], [4, 6, "leaves"]
]
new = [4, 5]

def distance(a, b):
    return ((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) ** 0.5

ranked = sorted(stores, key=lambda s: distance(s, new))
k = 3
nearest = ranked[:k]
for s in nearest:
    print(s[2], round(distance(s, new), 2))

votes = [s[2] for s in nearest]
leaves = votes.count("leaves")
print("Prediction:", "leaves" if leaves > k / 2 else "stays")

Result: two of the 3 nearest stores left, so the model predicts the new store will leave. Now it's a management decision: a call from the sales rep, or a special offer.

Watch outPick an odd k (3, 5, 7) so the vote can't tie between two classes. A very small k (1) is swayed by one odd store; a very large k drifts toward the overall majority.
Watch outScaling matters: if one feature is in riyals (thousands) and another in weeks (1–10), the riyals dominate the distance. So we bring every feature to the same range (say 0 to 1) before measuring distance.

Exercises

Exercise 1 · Calculation

What is the distance between store (3, 4) and store (0, 0)?

Exercise 2 · Multiple choice

Why choose k = 3 or 5 rather than 4?

Exercise 3 · Python · graded by running

Write a function distance(a, b) that returns the distance between two points of two numbers each, like [3, 4]. (The checker calls it and prints the rounded result.)

Spotted a mistake on this page, or have an idea to improve the site?