pythonintermediate

TabPFN Few-Shot Tabular Classification

Classify tabular data in seconds without hyperparameter tuning using TabPFN's in-context learning.

python
from tabpfn import TabPFNClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
import numpy as np

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42, stratify=y)

# Use a tiny training set — TabPFN excels at few-shot learning
X_few, _, y_few, _ = train_test_split(X_train, y_train, train_size=30, stratify=y_train, random_state=42)

model = TabPFNClassifier(device='cpu', N_ensemble_configurations=8)
model.fit(X_few, y_few)

predictions = model.predict(X_test)
print(f'Few-shot accuracy ({len(X_few)} examples): {accuracy_score(y_test, predictions):.4f}')
print(classification_report(y_test, predictions, target_names=['setosa','versicolor','virginica']))

Use Cases

  • few-shot classification
  • tabular ML
  • no hyperparameter tuning

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.