Unsupervised learning
Constrained and Semi-Supervised Clustering
Incorporate must-link, cannot-link, seed, and pairwise guidance into clustering while auditing contradictions and bias.
By the end you can
- Define must-link, cannot-link, seed, and metric-learning forms of clustering guidance
- Explain how constraints change feasible partitions or the learned geometry
- Detect inconsistent, non-transitive, and biased constraint sets
- Design a constraint collection and evaluation protocol with held-out guidance
Key idea
Do not evaluate on the constraints used to fit the clustering
Satisfying every training constraint perfectly says little about generalization to new pair judgments. A model can memorize the provided relations while damaging the broader structure. Pourrajabi and five colleagues put that objection on the record in 2014. They set out the ways a semi-supervised clustering is evaluated in practice, then rule one of them out by name. The banned setup is “use all data”: fit on the constraints, then score on the same constraints. “This approach is not recommended, as it clearly violates the basic principle that a learned model should not be validated using supervised training data.” Their replacement is CVCP, “Cross-Validation for finding Clustering Parameters”. Constraints are drawn from n−1 training folds, and the clustering is then scored as a classifier of the held-out fold's constraints.
The double use is not a neutral shortcut. It selects for the wrong model. In the COBS experiments, Van Craenendonck and Blockeel scored semi-supervised algorithms on the same 50 constraints those algorithms had been given, and it “does not produce better results”. Their results discussion names the mechanism: “This is caused by using the same constraints twice: once within the semi-supervised algorithms, and once to evaluate the algorithms and select the best-performing one. Obviously, algorithms that overfit the given constraints will get selected in this manner.” So they compute ARI only on objects not involved in any constraint. Basu and colleagues had taken the same precaution in 2004, computing NMI and pairwise F-measure only on a 10% test fold from which no constraints were supplied.
Hold out constraints by entity, domain, or time. Then check that what you held out is genuinely independent of what you kept. Evaluate unconstrained neighborhoods and downstream outcomes too, so that constraint satisfaction does not become the sole objective.
Two independent groups scored only on pairs the algorithm was never given; that is the protocol, not a refinement of it.
A little supervision that overruled the data
Twenty road segments of GPS traces, one algorithm, two runs. With must-link and cannot-link pairs enforced it averaged 98.6% on the Rand index. Without them, 58.0% on exactly the same segments. On that task the supervision was not breaking ties. It was carrying the result. The same literature reports the opposite outcome as well, from a paper one of the same authors co-wrote: constraint sets that “can actually decrease algorithm performance”.
Limited supervision can focus structure on a real task. It can also turn an unsupervised system into a brittle reflection of sparse, biased judgments. A fully satisfied constraint set does not tell you which of the two you are holding.
Constraints are training evidence and need their own sampling design.
Visual
Ways to guide a clustering without full labels
Guidance can restrict assignments, initialize groups, or reshape the distance itself. Those are not three settings of one dial. Xing and colleagues measured the gap in 2002. On a synthetic three-dimensional two-class set, K-means scored 0.4975 accuracy, and constrained K-means, enforcing the supplied pairs as hard constraints, scored 0.5060. Hand those same pairs to a Mahalanobis metric instead, learn the metric, cluster under it, and the result was accuracy 1. Their second example ran 0.4993, 0.5701 and 1. They state the reading plainly: “As shown by the accuracy scores given in the figure, both K-means and constrained K-means failed to find good clusterings. But by first learning a distance metric and then clustering according to that metric, we easily find the correct clustering separating the true clusters from each other.”
The difference is reach. A hard constraint moves the pair it names. A learned metric moves every point near that pair. Davidson and Ravi restate the finding — “The results of Xing and collaborators ... showed that learning a distance function from constraints and then using this distance function to cluster the data and not enforcing the constraints produced better results than just enforcing the constraints” — and then supply the counter-case. It is the same reach running the wrong way. Set k below the number of labels the constraints came from, and “the vast majority of constraints will effectively be ignored in the final clustering and ... the learnt distance function generally performs worse than the Euclidean distance function”.
Must-link pairs
Require two observations to share a cluster.
Cannot-link pairs
Forbid two observations from sharing a cluster.
Seeds or exemplars
Anchor selected observations to provisional groups.
Metric learning
Learn a representation where approved pairs move closer and rejected pairs move apart.
Constrained objective
Add penalties or feasibility rules to a base clustering algorithm.
Guidance changes either the feasible partitions or the geometry used to search them.
Comparison
Hard constraints, soft penalties, and learned metrics
The mechanisms differ in how they handle contradiction and uncertainty. For hard constraints that difference is a proved result rather than a caution. Davidson and Ravi asked the feasibility question in 2007: does any partition satisfy the given relations at all? Under must-link constraints alone the question is cheap, a transitive-closure computation in O(n+m). Add cannot-link and it stops being cheap. “The feasibility problem for clustering under ML constraints is in P while clustering under CL only and ML and CL is NP-complete, as can be shown by a reduction from graph coloring”. An unrelated group of five restates it: “It is NP-complete even only to determine whether an instance of the CL-constrained clustering problem is feasible”, obtained “via a reduction from the k-coloring problem”.
Repair is no easier than detection. Fixing an infeasible clustering is NP-complete as well, and no ρ-approximation exists for deleting the minimum number of constraints to restore feasibility. A few failures are trivial to spot: ML(a,b) together with CL(a,b), or CL(a,b), CL(b,c) and CL(a,c) with k ≤ 2. In general, though, the contradiction check a hard-constraint pipeline needs is exactly the problem those theorems call intractable. Soft penalties and learned metrics are not softer standards. They are ways of not having to answer that question exactly.
Hard constraints
Reject any partition that violates specified relations.
- Clear semantics
- Can make the problem infeasible
- Sensitive to annotation errors
- Needs contradiction detection
Soft penalties
Add a cost for violating guidance.
- Tolerates noisy feedback
- Requires penalty weights
- Can ignore weak constraints
- Makes tradeoffs explicit
Metric learning
Changes distances before or during clustering.
- Can generalize beyond labeled pairs
- May overfit sparse supervision
- Can distort unrelated regions
- Needs held-out pair evaluation
Analogy
Assembling a seating plan with partial requests
Some guests ask to sit together and others ask to be kept apart. The seating plan has to honor both. A few requests help, but contradictory or one-sided preferences can make the plan impossible or unfair.
Clustering constraints are translated into geometry or optimization rather than negotiated socially. Their effect can propagate to unlabeled observations in ways reviewers did not anticipate. And, unlike a seating plan, no one is in the room to notice that two requests cannot both be met.
Sparse guidance can reshape the whole partition, not only the labeled pairs.
Steps
Build a defensible constrained-clustering study
Treat annotation, consistency, fitting, and validation as separate stages. Two of those stages have published protocols and measured comparisons behind them, not advice.
Step 2, sampling. How the pairs are chosen matters as much as how many there are. In 2004 Basu and colleagues selected pairs with a two-phase Explore/Consolidate scheme built on farthest-first traversal, and compared it against randomly drawn pairs. The data sets were News-sim3 (300 documents, 3,225 dimensions), News-diff3 (300 documents), News-all20 (2,000 documents, 16,089 dimensions), a 400-document Classic3 subset and Iris (150 points). Learning curves ran out to 1,000 pairwise constraints — 5,000 for News-all20 — under 10-fold cross-validation. The result: “For the active algorithms, we consistently get significant improvements over the non-active algorithms, for all datasets we have considered.” On News-all20 the unstable opening of the curve is behind them after roughly the first 20 queries. The COBRA authors summarize the same finding — “Basu et al. [2004a] introduce a strategy to select the most informative constraints, prior to performing a single run of a constraint-based clustering algorithm. They show that active constraint selection can improve clustering performance” — while noting that in practice queried pairs “are often selected randomly”.
Steps 3 and 5, consistency and held-out validation. These are one problem seen twice, because pairwise constraints generate other constraints. COBRA is built on exactly that property: “constraint transitivity and entailment ... two properties that allow deriving additional constraints from a given set of constraints”. Pourrajabi and colleagues show what it does to a naive split: “For example, suppose a training fold contains the constraints must-link(A,B) and cannot-link(B,C). If the test fold contains the constraint cannot-link(A,C), this is information that was, implicitly, already available during the clustering process even though only the explicit constraints in the training folds were given.” Splitting a constraint set at random therefore “can lead to significantly underestimating the true classification error”. Their fix is structural: cut the constraint graph so that the folds are genuinely non-overlapping. The entailment that leaks across your split is the same entailment that makes A–B–C contradictions possible. That is why step 3 and step 5 share a piece of code.
1. Define relation semantics
Explain exactly what must-link and cannot-link mean for the task.
2. Sample informative pairs
Cover common, rare, boundary, and disputed regions rather than only easy examples.
3. Check consistency
Detect direct contradictions, transitive conflicts, and impossible hard constraints.
4. Fit alternatives
Compare hard, soft, and metric-learning approaches against an unconstrained baseline.
5. Validate held-out guidance
Measure pair agreement, partition stability, fairness slices, and task utility.
Example
Constraint failures that deserve explicit tests
Pairwise guidance can conflict locally, globally, or through missing coverage. The region nobody constrained is where the worst outcome sits. In 2017 Chierichetti and colleagues clustered three UCI data sets: Diabetes (1,000 records, balancing gender), Bank marketing (1,000 records, marital status) and 1994 Census/Adult (600 records, gender). Here is what the classical objectives did with an attribute no one had constrained: “Observe that the balance of the solutions produced by the classical algorithms is very low, and in four out of the six cases, the balance is 0 for larger values of k, meaning that the optimal solution has monochromatic clusters.” The Bank k-median instance produced three monochromatic clusters from k = 12 onward. Imposing balance ≥ 0.5 through fairlets restored it, and the paper prices the repair: “the balance comes with a corresponding increase in cost”. In 2019 Backurs and five colleagues re-ran fair clustering on the same three data sets, reporting fair k-median cost at k = 20 of 4,149 on Diabetes and 2.41×10⁷ on Census, against roughly 9,909 and 3.65×10⁷ for the 2017 method.
- Direct contradiction: The same pair is marked both must-link and cannot-link by different reviewers — ML(a,b) with CL(a,b), the failure Davidson and Ravi list as trivially infeasible.
- Transitive conflict: A must-links to B and B must-links to C, while A cannot-links to C — the entailment that also leaks a test-fold constraint into a training fold.
- Coverage bias: Constraints concentrate on easy common cases and provide no guidance near rare boundaries, which is the gap Explore/Consolidate sampling was built to close.
- Temporal mismatch: Two accounts must-link under an old product taxonomy that no longer reflects current behavior.
- Protected proxy: Reviewers repeatedly separate pairs by a feature correlated with a sensitive attribute — or nobody constrains that attribute at all and the optimum segregates on it, as in four of the six k-center and k-median runs above, where the balance of the returned clusters fell to 0.
Semi-supervised clustering should admit where supervision enters
Constraints can make groups more relevant to a domain. They also reduce the claim that the result emerged from data alone. On the lane-finding task the constraints were most of the answer — 98.6% against 58.0% — and a result that dependent on human judgment has to be reported as such. That is not a weakness when it is documented honestly.
Preserve the constraint source, reviewer disagreement, coverage, and version. Record how the held-out set was cut, since a random split of a constraint graph is not a split. The final cluster model is inseparable from that human evidence.
Human guidance is part of the model and part of its accountability record.
Case
98.6% against 58.0% on the same twenty road segments
Constraints were tested on real GPS traces in 2001, on a lane-finding task. Wagstaff and colleagues report accuracy over 20 road segments in their Table 2: “COP-KMEANS consistently outperformed the unconstrained k-means algorithm, attaining 100% accuracy for all but three data sets and averaging 98.6% overall. The unconstrained version performed much worse, averaging 58.0% accuracy.” Accuracy there is the Rand index against the true lane labels.
Constraints were not decoration. They carried the result. Which is precisely why the same constraints cannot then be used to score it.
Figure
Case
The constraint sets that made performance worse
A 2006 study asked whether every constraint set helps. Two of its three authors appear elsewhere in this lesson: Kiri Wagstaff, who ran the lane-finding experiment, and Ian Davidson, who proved the feasibility results. What they found is that “constraint sets vary significantly in how useful they are for constrained clustering; some constraint sets can actually decrease algorithm performance”.
Their answer is not to collect more pairs. It is to measure the ones already collected: they propose “two quantitative measures, informativeness and coherence, that can be used to identify useful constraint sets”. A constraint count is not a quality claim.
Key takeaways
- Must-link and cannot-link relations restrict or penalize candidate partitions, and on 20 GPS road segments that guidance moved Rand-index accuracy from 58.0% to 98.6%.
- Hard constraints can become infeasible when judgments conflict: Davidson and Ravi place the feasibility test in P under must-link alone and NP-complete once cannot-link is present, with repair NP-complete as well.
- Metric learning can generalize guidance and also distort uncovered regions — 0.4975 to accuracy 1 in Xing's experiments, against cases where the learnt distance function performs worse than Euclidean distance.
- Constraint collection needs representative sampling and disagreement tracking; Explore/Consolidate selection beat randomly drawn pairs on every data set Basu and colleagues tested.
- Held-out pair judgments are necessary for evaluating generalization, and transitivity and entailment mean a random split of a constraint set does not produce independent folds.
- The constraint set belongs in the model's lineage and accountability record, including the measures — informativeness and coherence — that say whether it was worth having.