Clustering
Define the two-cluster segmenter input contract
Page 2 hardens the boundary around the two-cluster customer segmenter so bad inputs fail before the interesting algorithm runs.
1Learn the idea
Read
Define what may enter
The accepted input remains: 2D numeric customer features and chosen k. Keep parsing and normalization in functions that do not score, train, or call a model. That split lets a test fail the boundary without blaming the core logic. The user-facing decision stays: group customers by spend/visits without pretending clusters are ground-truth labels.
Read
Reject at the boundary
def scale(points):
xs=[p[0] for p in points]; ys=[p[1] for p in points]
return [((x-min(xs))/(max(xs)-min(xs)),(y-min(ys))/(max(ys)-min(ys))) for x,y in points]
print(scale([(2,18),(15,82)]))
Expected evidence: ((0.0, 0.0), (1.0, 1.0)). If the contract is silent on a bad value, later debugging will look like an algorithm bug when it is really a data bug.
Read
Keep transforms testable
Write one assertion for a neighboring valid input to the two-cluster segmenter so tightening the boundary does not over-reject. Document field names and types the way a teammate would need them on day two of clustering-basics—not as comments you plan to delete.
Read
Lab notebook: name the fields
List every field in small 2D customer matrix and mark each as required, optional, or forbidden. Required fields must fail loudly when missing; optional fields need defaults you can quote in a test; forbidden fields (secrets, raw PII, path escapes) must never be accepted silently. This list is the contract for the two-cluster segmenter.
Add one sentence about encoding, units, or timezones if relevant to 2D numeric customer features and chosen k. Contracts that ignore units create “correct” programs that still ship wrong decisions when someone tries to group customers by spend/visits without pretending clusters are ground-truth labels.
Read
Worked judgment
Write the error string you want for the most likely bad input. Prefer ValueError('threshold out of range')-style messages over generic invalid input. The contract’s job is to make k larger than n, or scaling skipped so one feature dominates harder to confuse with a model or algorithm bug later.
Read
Why this stage matters for the two-cluster segmenter
At the data contract 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 malformed inputs rejected with field-named errors 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
Invent one malformed input that the two-cluster customer segmenter might accidentally accept. Predict the exception or rejection message. After you run the contract code, compare your prediction with the real failure text.
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.