Clustering
Debug k larger than n in the two-cluster segmenter
Page 5 reproduces and repairs the characteristic failure of the two-cluster customer segmenter: k larger than n, or scaling skipped so one feature dominates.
1Learn the idea
Read
Reproduce before you repair
Do not start with a speculative fix for the two-cluster segmenter. Force the failure on purpose, save the before output, then change one cause at a time. Retries are allowed only for transient conditions—not for bad input that will fail forever on clustering-basics.
Read
Force the failure
def mean(group, old):
return old if not group else tuple(sum(v)/len(group) for v in zip(*group))
print(mean([], (.25,.75))) # preserve center instead of dividing by zero
Expected evidence: the old center is returned for an empty group. If you cannot reproduce on demand, you do not yet control the failure mode for clustering-basics.
Read
Repair with a reviewable diff
After repair, rerun the exact reproduction command. Keep the failing fixture as a regression seed for the observability page. For the two-cluster customer segmenter, remember the claim you are restoring: k=2 assignments are stable for the fixture and cluster centers are printable.
Read
Lab notebook: reproduce on command
Store a one-command reproduction for: k larger than n, or scaling skipped so one feature dominates. The command should use small 2D customer matrix or a minimal mutant of it. Paste the failing output into notes/failure-before.txt (or your shell scrollback as copied text). After the fix, paste notes/failure-after.txt and keep both.
Retries belong only on transient faults. If the failure is bad input, a bad allowlist, or a logic bug in the two-cluster segmenter, retrying will amplify cost without repairing trust around group customers by spend/visits without pretending clusters are ground-truth labels.
Read
Worked judgment
Classify the failure as prevent, detect, contain, or recover—using this lab’s language, not a generic poster. For clustering-basics, the first fix should usually be detect+prevent at the boundary, because k larger than n, or scaling skipped so one feature dominates is cheaper to stop early than to explain in production prose.
Read
Why this stage matters for the two-cluster segmenter
At the debugging stage for clustering-basics, the job is narrower than finishing a product demo. You are creating one progressive evidence piece about small 2D customer matrix that later pages inherit without redefining success. Keep that fixture small enough to inspect by hand, keep outputs copy-pasteable as text, and refuse to narrate this baseline as if it were a production SLA: random two-group assignment inertia for comparison.
For this page specifically, success looks like before/after evidence for the characteristic failure while still centering the user decision to group customers by spend/visits without pretending clusters are ground-truth labels. If you cannot point to a file, command, or assertion that proves that for the two-cluster segmenter, stay on this page instead of advancing.
Go deeper
Before you start
Why this matters
Describe the smallest fixture that triggers k larger than n. Predict the first visible symptom (exception, wrong label, silent empty success). You will compare that prediction with the reproduction below.
Related lessons
Check your understanding
Page assessment
Answer from memory. Completion is saved from this evidence, not from opening the next page.
All responses are required.