pythonintermediate
TabPFN Few-Shot Tabular Classification
Classify tabular data in seconds without hyperparameter tuning using TabPFN's in-context learning.
pythonPress ⌘/Ctrl + Shift + C to copy
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.
typescriptbeginner
Few-Shot Prompt Template
Build structured few-shot prompts with examples, system instructions, and output format constraints.
Best for: Consistent AI outputs
#prompts#few-shot
typescriptintermediate
AI Text Classification with Prompts
Classify text into categories using structured prompting with confidence scores and explanations.
Best for: Automated support ticket routing
#classification#prompt-engineering
pythonintermediate
Text Classification with Hugging Face
Fine-tune or use pre-trained Hugging Face models for text classification.
Best for: Sentiment analysis
#ai#huggingface
pythonintermediate
LangChain Few-Shot Prompt Examples
Improve LLM accuracy with dynamic few-shot examples selected by semantic similarity.
Best for: few-shot learning
#langchain#few-shot