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.
# [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.