Imagine ninety percent of our training tickets were complaints. The model would learn a lazy trick. When in doubt, say complaint. So before training, we count every label.
A dataset is balanced when every class has a similar number of examples. A class is one possible label, like stock, or return. When one class is rare, the model sees it too few times to learn it. And it may hardly ever predict it.
Here's how to count. Counter, from Python's collections module, counts how often each value appears. We count the intents, and the books. Then we print a little bar for each intent. Run it. Our five intents each have just over a hundred examples. Nicely balanced. And even the rarest book appears sixty times.
app.pypython
from collections import Counterfrom datasets import load_datasetdata = load_dataset("json", data_files="tickets_train.jsonl")["train"]intents = Counter(data["intent"])books = Counter(data["book"])for name, n in intents.most_common(): print(f"{name:13} {n:4} {'#' * (n // 5)}")print()print("Books:", len(books), " smallest:", books.most_common()[-1])
If your data is not balanced, you have three options. Collect, or generate, more examples of the rare classes. Remove some examples of the common classes. Or repeat the rare examples, which is called oversampling. Collecting more real data is usually best.
Let's recap. A balanced dataset has similar counts for every class. Rare classes are learned badly. Counter shows the counts in one line. And collecting more rare examples is the best fix. Next, bias and safety.