Sample data for training and testing

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

Data Import/Export · Notebook · Updated

Colab runs it in your browser. If it calls a model provider, it asks for that provider's API key in a cell.

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
terminal
pip install pixeltable
Pixeltable calls
pxt.create_table()insert()sample()

A look at the code

data-sampling.ipynb
# 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)

One cell of the notebook, copied as it is. Open it in Colab for the rest.

Browse all 103