Concepts / Splitting Data into Train and Test Sets

Splitting Data into Train and Test Sets

Duplicated data can skew the data distribution and bias what a model learns, because repetition gives one pattern disproportionate weight.

  • Programming

A Dataset That Repeats Itself

A dataset is a collection of records that a model learns from, and it is easy to assume that a record appearing twice is just a little extra material. Repetition, however, changes what a model learns. The same record that shows up many times carries its pattern with much more weight than a record that appears once. This article follows how that weighting happens, how a copy can leak from the training side of a split into the test side, and how to decide what counts as a duplicate in the first place.

What do you think happens?

A small product dataset has a red pencil priced at 20 dollars that appears several times, while other items appear only once. What would a model take away from the repeated red pencil records?

  • That pencils are expensive

  • That red-colored items should be expensive

  • Nothing, because repeated records are harmless

Reveal answer
Answer:

The model could conclude that red-colored items should be expensive.

The model is not reasoning about pencils or colors in general. It is reacting to how often one pattern shows up, so the repeated red pencil entries tilt what it learns.

Following the Red Pencil Records

A dataset is a sample of some part of the world. When certain records are copied, the sample tilts toward them, so the pattern they carry looks more common than it really is. In the red pencil example, the red pencil entries take up a large share of the data, while every other item appears only once. The diagram below compares the situation before and after the repeated records are removed. Watch the red pencil entries, the pattern that changes.

Before the copies are removed, the red pencil pattern dominates what the model sees, and the model may treat red as a signal of high price. After the copies are removed, the red pencil appears once, like every other item, and no single pattern is overweighted. The model is still learning from the same kinds of items, but the sample no longer leans on one of them.

How a Copy Leaks into the Test Set

Models are typically judged on data they did not learn from. A train and test split exists to keep those two groups separate: the training set is what the model learns from, and the test set presents inputs it has not effectively seen. Duplication can break that separation. When duplicated data is split into training and test sets, one example may land in the training set while its duplicate lands in the test set.

Definition

Contamination is the situation in which the test set contains data the model has already encountered during training, usually because a copy of a test example was placed in the training set. A score on a contaminated test set can look better than the model's real ability on new inputs.

Follow the path in the diagram. The original record is split, and one copy goes to training while its twin goes to testing. During training, the model meets the answer through the first copy. When the model is later scored on the test set, the second copy looks familiar, so the score reflects memory of an example rather than performance on new inputs.

The effect is not only a matter of evaluation. Several studies report a negative effect of duplicated training data on model performance. One Anthropic study cited in the source found that repeating 0.1% of the data 100 times could make an 800M parameter model perform like a 400M parameter model, even though the other 90% of training tokens remained unique.

Duplication can also cost resources even when it does not visibly hurt the final model. Repeated material consumes training time and compute without adding new information, so duplicates can waste resources even when the model looks acceptable.

Whole, Intra, and Cross-Document Copies

When the data is a collection of documents, duplication usually takes one of three forms. Recognizing which form you are dealing with helps you decide how to look for it, because some duplications are harder to detect than others. A repeated paragraph inside one file and a quote scattered across many files call for different search strategies, so it helps to ask first where the copies are likely to live.

Read the diagram column by column. Each column names one form, and the two boxes inside it are the pair of text units being compared. The scope grows as you move from left to right in thinking about the search: a whole document is the largest unit, while a quote can be a small fragment that appears in many places.

Three Choices That Define a Duplicate

Finding duplicates starts with a definition, and the definition is a choice. Nothing about two records makes them duplicates on its own; you decide how to compare them. Three decisions shape the result: the level at which you compare items, the rule for how closely they must match, and whether order matters.

  • The comparison level: whole documents, paragraphs, sentences, or tokens. Each level catches a different kind of repetition.
  • The match rule: whether two items must be identical, or whether a threshold such as an 80% overlap is enough.
  • The role of order: whether two lists with the same items in a different order count as duplicates, depending on what you intend to measure.

The match rule and the order question are where many duplicate decisions go wrong. An exact match finds only identical items, while a threshold finds near-copies as well. Whether order matters is not a technical fact about the data; it depends on what you want the comparison to measure. A deduplication step built for one purpose may be inappropriate for another.

Tracing a Duplicate Decision

The flowchart below turns the three choices into an ordered sequence of checks. Given two records, you pick a comparison level and a match rule, then ask whether order matters. The answer to that last question decides which comparison runs, and that is the step where reordering the items can change the result.

Worked example

Two Lists with the Same Items

Record A is the list red pencil, blue eraser, desk lamp. Record B is the list desk lamp, blue eraser, red pencil. Do they count as duplicates?

  1. 1
    Choose the comparison level

    Each record is a whole list, so the comparison unit is the full record.

  2. 2
    Choose the match rule

    Use an exact match on the items each list contains.

  3. 3
    Decide whether order matters

    Suppose the goal is to find records that list the same products in the same sequence. Then order matters.

  4. 4
    Run the sequence comparison

    Position one holds red pencil in Record A and desk lamp in Record B. They differ, so the records are not duplicates under this rule.

  5. 5
    Change the order decision

    Suppose instead the goal is to find records that contain the same products. Then order does not matter, and the item sets are identical, so the records are duplicates under this rule.

Result

The same two records are not duplicates when order matters and are duplicates when it does not. The outcome depends on the order decision, which is made before any comparison runs.

The example shows why the order decision must be made deliberately. Nothing in the two records settles it. The same pair can be a duplicate for one purpose and distinct for another, so the definition belongs to the task you are preparing the data for.

Mistakes That Let Copies Slip Through

  • Common mistake

    Treating repetition as harmless extra material

    What it looks like

    Keeping every copy of the red pencil record because each one is valid data on its own.

    Why it fails

    Each repeated copy shifts the sample toward one pattern, so the model may learn that pattern is more common than it really is.

    Do this instead

    Ask whether the repeated pattern reflects the world or only the way the data was copied.

  • Common mistake

    Assuming duplicates are only identical whole files

    What it looks like

    Checking only whether two files match exactly, and missing a paragraph repeated inside one file.

    Why it fails

    Intra-document duplication lives inside a single document, and cross-document duplication may match only a fragment.

    Do this instead

    Decide the comparison level first, then search at that level for the form of duplication you expect.

  • Common mistake

    Splitting before checking for copies

    What it looks like

    Dividing the data into training and test sets, then discovering that a record appears on both sides.

    Why it fails

    Once one copy sits in training and its twin sits in testing, the test set is contaminated and its score can overstate real performance.

    Do this instead

    Look for duplicates before the split so that a copy cannot land on both sides.

  • Common mistake

    Choosing the match rule without thinking about near-copies

    What it looks like

    Requiring exact equality when the data contains texts that differ only slightly.

    Why it fails

    An exact match misses near-copies, while an overly loose threshold may flag records that are genuinely distinct.

    Do this instead

    Decide explicitly whether identical text is required or whether an overlap threshold such as 80% is appropriate for the task.

  • Common mistake

    Ignoring order without deciding whether it matters

    What it looks like

    Treating a list in a different sequence as automatically the same, or as automatically different, without a reason.

    Why it fails

    Whether order matters depends on what the comparison is meant to measure, so the default can silently change the result.

    Do this instead

    Write down whether order matters for your purpose before running the comparison.

Practice Deciding Duplicates

MEDIUM

Two records are compared. Record A is a document containing the paragraph beginning with the same sentence twice in different places. Record B is a separate document containing a quote that also appears in Record A. Name the duplication form for each pattern, explain which comparison level you would use to find it, and say whether you would split the data before or after looking for copies, giving one reason.

Hints
  • The repeated sentence sits inside one document, so think about which form compares passages within a single document.
  • The quote crosses two documents, so think about which form spans several files.
  • Consider what happens to a copy that lands on both sides of a split.

Key Takeaways

  • Repeated records give one pattern disproportionate weight, which can skew the data distribution and bias what a model learns.
  • When a duplicate is split across training and test sets, the test set becomes contaminated and its scores can overstate real performance.
  • Duplication takes whole-document, intra-document, and cross-document forms, and each form calls for a different search strategy.
  • What counts as a duplicate depends on the comparison level, the match rule such as exact match or an overlap threshold, and whether order matters.

More concepts starting with S