Skip to content
C.W.K.
Lesson 02 of 08 · published

Dataset vs DatasetDict, Features, and Schema

~24 min · datasets, schema

Level 0Scout
0 XP0/50 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Two classes, one mental model

Dataset is a single Arrow-backed table with named columns and rows. DatasetDict is just {split_name: Dataset} with convenience methods that broadcast operations to every split. Most operations — map, filter, shuffle — you can also call on a DatasetDict, and they apply to all splits; select is the exception.

Features are the schema

ds.features is the schema dict. Each column has a Value, ClassLabel, Sequence, Audio, Image, or nested type. For classification, ClassLabel carries the label-int-to-name mapping — so ds.features['label'].int2str(1) yields 'positive'. Treat the features dict like a database schema: type-aware, queryable, and the source of truth for what the data means.

Code

Inspect the schema·python
from datasets import load_dataset

ds = load_dataset("stanfordnlp/imdb")
print(ds["train"].features)
# {'text': Value(dtype='string'), 'label': ClassLabel(names=['neg', 'pos'])}

# Convert label int → name
print(ds["train"].features["label"].int2str(1))  # 'pos'

# Slice access
sample = ds["train"][:3]  # dict of column → list
print(sample["label"])
Operations broadcast on DatasetDict·python
from datasets import load_dataset, DatasetDict

ds = load_dataset("stanfordnlp/imdb")  # DatasetDict

# shuffle, map and filter called on a DatasetDict apply to every split
shuffled = ds.shuffle(seed=42)
long_only = ds.filter(lambda ex: len(ex["text"]) > 200)
print({k: len(v) for k, v in long_only.items()})

# select exists only on Dataset, so call it per split
small = DatasetDict({k: v.select(range(1000)) for k, v in shuffled.items()})
print({k: len(v) for k, v in small.items()})

External links

Exercise

Load any classification dataset on the Hub. Print features, num_rows per split, and label distribution. Then create a stratified 80/20 train/dev split using train_test_split(stratify_by_column='label') and verify class balance is preserved.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign in — Please sign in to comment.

No comments yet — be the first.