---
title: "Sample data for training and testing"
description: "Create training, validation, and test splits with random or stratified sampling."
url: "https://www.pixeltable.com/notebooks/data-sampling"
updated: "2026-04-10"
---

# Sample data for training and testing

Create training, validation, and test splits with random or stratified sampling.

- **Type:** Notebook
- **Group:** Data Import/Export

## How it works

You use query.sample() to create random subsets, with optional stratification for balanced class distribution.

## What it covers

- Random sampling with sample(n=...)
- Percentage-based sampling with sample(fraction=...)
- Stratified sampling with stratify_by=

## What it uses

- **Install:** `pip install pixeltable`
- **Pixeltable calls:** `pxt.create_table()`, `insert()`, `sample()`

## A look at the code

```python
# Create a dataset with labels
data = pxt.create_table(
    'sampling_demo/data',
    {'text': pxt.String, 'label': pxt.String, 'score': pxt.Float},
)

# Insert sample data with imbalanced classes
samples = [
    {'text': 'Great product!', 'label': 'positive', 'score': 0.9},
    {'text': 'Love it', 'label': 'positive', 'score': 0.85},
    {'text': 'Amazing quality', 'label': 'positive', 'score': 0.95},
    {'text': 'Best purchase ever', 'label': 'positive', 'score': 0.88},
    {'text': 'Highly recommend', 'label': 'positive', 'score': 0.92},
    {'text': 'Fantastic!', 'label': 'positive', 'score': 0.91},
    {'text': 'Terrible', 'label': 'negative', 'score': 0.1},
    {'text': 'Waste of money', 'label': 'negative', 'score': 0.15},
    {'text': 'It is okay', 'label': 'neutral', 'score': 0.5},
    {'text': 'Average product', 'label': 'neutral', 'score': 0.55},
]
data.insert(samples)
```

## Open it

- **Open in Colab:** https://colab.research.google.com/github/pixeltable/pixeltable/blob/release/docs/release/howto/cookbooks/data/data-sampling.ipynb
- **Docs page:** https://docs.pixeltable.com/howto/cookbooks/data/data-sampling
- **Source on GitHub:** https://github.com/pixeltable/pixeltable/blob/release/docs/release/howto/cookbooks/data/data-sampling.ipynb
