Clustering
Build the first working two-cluster segmenter
Page 3 implements the shortest complete path for the two-cluster customer segmenter with inspectable intermediate values.
1Learn the idea
Read
Implement the minimal working path
Build only what the claim requires: k=2 assignments are stable for the fixture and cluster centers are printable. Prefer boring, deterministic code over frameworks you cannot yet explain. Run the path twice; identical output on this fixture is a feature, not a lack of creativity.
Read
Run the working path
from math import dist
pts=[(0,.0),(.08,.03),(.15,.06),(.77,.83),(.85,.91),(1,1)]
centers=[pts[0],pts[-1]]
for _ in range(10):
groups=[[p for p in pts if min(range(2),key=lambda j:dist(p,centers[j]))==i] for i in range(2)]
new=[tuple(sum(v)/len(g) for v in zip(*g)) for g in groups]
if new==centers: break
centers=new
print([len(g) for g in groups], centers)
Expected evidence: (3, 3) with centers near the two visible groups. Read each printed intermediate as part of the argument that the path works—not as decoration.
Read
Trace one input end to end
Narrate the journey from raw input to result for a single example from small 2D customer matrix. If you cannot name an intermediate, the implementation is still too opaque for this lab. Only after this path is solid should you generalize data sources or UI.
Read
Lab notebook: intermediates worth printing
While implementing the two-cluster segmenter, print or log at least three intermediates that map to the claim (k=2 assignments are stable for the fixture and cluster centers are printable). Good intermediates are values a teammate could recompute with a calculator or diff. Bad intermediates are framework traces you cannot explain.
Re-run with small 2D customer matrix twice. If the second run differs, either the path is nondeterministic (document the seed) or you have hidden global state—both are lab bugs until named.
Read
Worked judgment
Stop adding features once the path supports group customers by spend/visits without pretending clusters are ground-truth labels. Extra UI, extra tools, or extra models belong in later chapters. The mastery bar for this page is simply: a deterministic end-to-end path with intermediates.
Read
Why this stage matters for the two-cluster segmenter
At the implementation 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 a deterministic path with printed intermediates 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
Without running code, predict the final output for fixture small 2D customer matrix. Name one intermediate value that would prove the prediction. Then answer: what could look successful while actually being wrong at this stage for the two-cluster segmenter?
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.