Similarity Score Computation
Pairwise comparison is a deduplication method that computes the similarity score of each example against every other example in the dataset.
Why Duplicate Examples Need Finding
Imagine a training dataset for an AI model that contains thousands of text examples. Some of them may be copies of one another, or near-copies that differ only in a few words or spacing. Duplicate examples can quietly distort what a model learns, which is why deduplication is part of preparing data.
Before you can remove a duplicate, you need a way to find out which examples resemble each other. Pairwise comparison is one direct strategy for that search. It is a deduplication method that computes the similarity score of each example against every other example in the dataset. The question it answers is simple: how do you decide that two examples are similar enough to count as duplicates?
What do you think happens?
If you have 5 examples and you score every example against every other example, how many distinct pairs get a score?
Reveal answer
10 pairs
Each distinct pair is scored once. For 5 examples the count is $\frac{5 \times 4}{2} = 10$. Working through the pairs by hand (A with B, C, D, E; then B with C, D, E; then C with D, E; then D with E) gives 4 + 3 + 2 + 1 = 10 as well.
Tracing Four Examples Pair by Pair
The key word in pairwise comparison is every. The method does not look for a single representative of a group, and it does not skip examples that seem unlikely to match. Each example takes a turn being the one under inspection, and it is scored against all of the other examples.
Consider four generated example rows, labeled A, B, C, and D. Start with A. It is scored against B, then against C, then against D. Now B is the one under inspection. Its pair with A has already been scored, so B only needs scores against C and D. Then C needs only its score against D. Each pair appears exactly once in the trace.
Notice that A is involved in three pairs, B in three, C in three, and D in three. Every example has one line leaving it for each of the other examples. Because a pair is scored once and covers both examples in it, the picture is the same no matter which example you start from. Nothing is left out, and nothing is repeated.
Two details are worth holding onto. First, the score is computed for a pair, so the similarity of A to B is the same kind of result as the similarity of B to A. Second, once each pair has a score, those scores can be used to decide which examples count as duplicates. The source describes the scoring step. How a high or low score is turned into a removal decision is a separate choice that depends on the score and the dataset.
Counting Pairs Without Double Counting
Counting the pairs for four examples was simple, and the same logic applies to any number of examples. If a dataset has n examples, the number of distinct pairs that get scored is given by the formula below. The formula counts each pair once, which matches the trace above, where B paired with A was not scored a second time.
- 1Start with n examples. Each one could be paired with the other n - 1 examples, giving n(n-1) ordered attempts.
- 2Each unordered pair was counted twice in that product, once as A then B and once as B then A, because the similarity of A to B equals the similarity of B to A.
- 3Dividing by 2 removes the double counting, leaving $\frac{n(n-1)}{2}$ distinct pairs.
Counting pairs for a dataset of 10 examples
A dataset has 10 examples. How many distinct pairs must be scored under pairwise comparison?
- 1Identify n
Here n = 10.
- 2Compute n(n-1)
$10 \times 9 = 90$ ordered attempts.
- 3Divide by 2
$\frac{90}{2} = 45$, so each unordered pair is counted once.
45 distinct pairs are scored.
Each step adds one new example, and that example must be scored against all the examples already present. Adding the fifth example adds 4 new pairs, the sixth adds 5, and so on. The total grows faster than the number of examples because every new example joins every existing one.
Where the Scores Come From: Four Methods
Pairwise comparison describes where the scores come from, but it does not specify how each score is calculated. The source names four methods for computing the similarity score of a pair of examples. Each method answers the question of how alike two examples are in a slightly different way.
- Exact match
- N-gram match
- Fuzzy match
- Semantic similarity score
The source points to Chapter 3 for the details of these methods. The important point for this article is that the pairwise framework stays the same regardless of which method you choose. Every example is scored against every other example, and the chosen method determines the number that each pair receives. Changing the method changes the scores, but it does not change which pairs are scored or how many there are.
Why the Pair Count Explodes
The main practical limitation of pairwise comparison is cost. The source notes that this approach can be expensive with large datasets. The formula from the earlier section shows why. The number of pairs grows with the square of the dataset size, so doubling the number of examples roughly quadruples the number of comparisons that must be computed.
| Examples (n) | Distinct pairs $\frac{n(n-1)}{2}$ | Examples doubled from previous row | Pairs compared with the previous row |
|---|---|---|---|
| 5 | 10 | No | Not applicable |
| 10 | 45 | Yes, from 5 | 4.5 times as many |
| 20 | 190 | Yes, from 10 | 4.2 times as many |
| 40 | 780 | Yes, from 20 | 4.1 times as many |
Doubling the dataset size roughly quadruples the number of pairs, and the ratio approaches 4 as n grows.
Each example contributes a term to the total, but each term also depends on the number of other examples. Data grows linearly while the pair count grows with the square of the dataset size. That is why a method that is manageable for a few hundred rows can become costly for a training set of many thousands of rows.
Mistakes Learners Make with Pair Counting
Common mistake Counting A vs B and B vs A as two separate comparisons
What it looks likeFor 4 examples, someone lists 12 comparisons by pairing each of the 4 examples with the other 3.
Why it failsEach distinct pair is scored once, and the similarity of A to B is the same kind of result as the similarity of B to A.
Do this insteadDivide n(n-1) by 2 to get the count of distinct pairs, giving 6 for 4 examples.
Common mistake Assuming an example is paired with itself
What it looks likeFor 4 examples, counting A vs A as one of the scored pairs.
Why it failsPairwise comparison checks each example against the other examples in the dataset, not against itself.
Do this insteadUse n(n-1)/2 as the count, which excludes self-pairs.
Common mistake Believing a different similarity method changes how many pairs are scored
What it looks likeSwitching from exact match to fuzzy match and expecting fewer comparisons.
Why it failsThe pairwise framework is the same regardless of method. The method determines the score each pair receives.
Do this insteadKeep the pair count fixed by dataset size, and treat the method as the choice of scoring function.
Common mistake Treating a computed score as an automatic removal decision. The source describes the scoring step, and deciding what counts as a duplicate is a separate choice.
Practice: Predict Before You Count
A dataset has 8 examples. Before calculating, predict roughly how many pairs pairwise comparison will score. Then compute the exact number using the formula n(n-1)/2, and explain in one sentence why the answer is not 8 times 8.
Hints
- Start with n(n-1) and then divide by 2.
- Remember that a pair is scored once, even though it involves two examples.
If a dataset grows from 10 examples to 20 examples, by roughly what factor does the number of pairs grow? Compute the two pair counts and compare them.
Hints
- Compute 10(9)/2 and 20(19)/2.
- Compare the ratio of the two counts to the ratio of the dataset sizes.
Key Takeaways
- Pairwise comparison is a deduplication method that scores each example against every other example, so no duplicate is missed because of where it sits in the dataset.
- Each distinct pair is scored once, so a dataset of n examples requires $\frac{n(n-1)}{2}$ pairwise scores.
- The four named methods for computing a pair's similarity score are exact match, n-gram match, fuzzy match, and semantic similarity score, with details in Chapter 3.
- Because the pair count grows with the square of the dataset size, pairwise comparison can become expensive on large datasets, and doubling the data roughly quadruples the comparisons.