An honest experiment: splits and baselines
Split related records by group, prevent data leakage, and compare your future model with a majority baseline.
Lesson contents
Why split the data
The training set, or train, contains examples used to change model parameters. Validation helps you choose settings. Test measures the final result after you've fixed your approach. If you select the model using test, the final assessment is no longer independent. Dividing data into these parts is called a split.
The project uses a 70/15/15 split. Split by source_id, because different
wordings of one incident may be nearly identical.
Leakage through related records
Consider these two lines:
source_id=inc-017 "У API выросло время ответа"
source_id=inc-017 "Ответ API стал медленнее"
If the first goes into train and the second into validation, the model partly sees the evaluation case during training. The metric rises, but the experiment doesn't show how the model handles new incidents.
All records with one source_id must remain in the same split. After splitting,
check both groups and normalized texts for overlap.
Reproducible shuffling
A seed sets a random number generator's initial state. The same seed and identical input should produce the same split:
import random
group_ids = ["inc-001", "inc-002", "inc-003"]
rng = random.Random(42)
rng.shuffle(group_ids)
print(group_ids) # ['inc-002', 'inc-001', 'inc-003']
shuffle changes the order of items in the list itself. This example only
shuffles three groups; the project function will split the full corpus.
Don't call the global random.shuffle without a seed. Such a test will
produce different results between runs.
Majority baseline
A baseline gives you a minimum reference point. A majority baseline always chooses the most frequent category in train:
from collections import Counter
train_labels = ["capacity", "capacity", "performance"]
counts = Counter(train_labels)
majority_label = counts.most_common(1)[0][0]
print(majority_label) # capacity
Counter counts occurrences. most_common(1) returns a list containing the
most frequent category and its count: [("capacity", 2)]. The first [0]
selects that pair; the second [0] takes the category name from it.
Validation and test must not take part in selecting the category. Otherwise, even the baseline receives information from a future evaluation.
A baseline doesn't prove the classifier is useful in production. It only shows whether the model outperformed a simple constant prediction on the current synthetic corpus.
Build the vocabulary only from train
Here, a token is a word extracted from text. The vocabulary assigns an integer
index to each known token. An unknown word gets the separate UNK index;
it is 1 in the classifier lesson:
vocab = {"api": 2, "отвечает": 3}
unk_id = 1
print(vocab.get("новое", unk_id)) # 1
A word that appears only in validation should receive UNK, rather than
its own vocabulary entry. Otherwise, the vocabulary has already incorporated
evaluation data.
The same rule applies to means, frequencies and any learned statistics. Calculate them from train, then apply them unchanged to validation and test.
Task
Implement the following in exercises/lesson05.py:
def split_and_baseline(records, seed=42):
...
The function returns (splits, majority_label). splits has the keys
train, validation, test; the values are lists of original records.
Import the existing group-splitting function stratified_group_split from
incident_classifier.data. Your exercise calls it with the supplied seed
and calculates the majority category using train only.
Requirements:
- the split is reproducible for the same seed;
- the group proportions are close to 70/15/15;
- each
source_idappears in exactly one split; - normalized texts don't overlap across splits;
- the majority category is calculated from train only;
- empty input and too few groups produce clear errors.
Hints
First, study the contract of stratified_group_split(records, seed=seed).
It already distributes groups and checks normalized texts for overlap.
Use collections.Counter for the majority label. Give it labels only from
splits["train"].
Check
python -m pytest -q tests/test_exercises.py --student exercises -k lesson05
The tests use repeated groups and a token that appears only in validation. It must not affect train.
Where to find the solution
The reference is in solutions/lesson05.py. The split implementation is in
src/incident_classifier/data.py. The exercise solution uses it rather than
creating another splitting implementation.
python -m pytest -q tests/test_exercises.py --student solutions -k lesson05
Common mistake
A random split by row may have the right proportions while putting similar
wordings of one case into different splits. Check the set of source_id
values after splitting.
Done reading?
Mark the lesson as read to move your track progress forward.
Lesson marked as done.
Practice
Fix a local lab. The agent checks your work and gives hints if you get stuck.
Full access to all labs
One payment, no auto-renewal · Terms
Progress saved in this browser
Clear your history or open this on your phone, and the marks will be gone. Leave your email and they'll move to your account. No password needed.
Done. moved to your account. You can see your progress on any device. My progress