pythonintermediate
CatBoost Gradient Boosting Training
Train a CatBoost model with automatic categorical feature handling and built-in cross-validation.
pythonPress ⌘/Ctrl + Shift + C to copy
from catboost import CatBoostClassifier, cv, Pool
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True, as_frame=True)
X['cat_feature'] = np.where(X['petal length (cm)'] > 3, 'long', 'short')
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = CatBoostClassifier(
iterations=200, learning_rate=0.1, depth=6,
cat_features=['cat_feature'], verbose=50, random_seed=42,
)
model.fit(X_train, y_train, eval_set=(X_test, y_test), early_stopping_rounds=20)
print('Test accuracy:', model.score(X_test, y_test))
print('Best iter:', model.best_iteration_)Use Cases
- categorical ML
- gradient boosting
- tabular classification
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonadvanced
Prepare a Fine-Tuning Dataset for OpenAI
Format, validate, and upload training data for OpenAI model fine-tuning.
Best for: Model customization
#ai#fine-tuning
pythonadvanced
Prepare Fine-Tuning Dataset for OpenAI
Build, validate, and upload a JSONL fine-tuning dataset for OpenAI GPT fine-tuning.
Best for: model customization
#openai#fine-tuning
pythonintermediate
MLflow Experiment Tracking in Python
Track ML experiments, log metrics, parameters, and artefacts with MLflow for reproducible training.
Best for: experiment tracking
#mlflow#experiment-tracking
pythonintermediate
PyTorch Lightning Training Loop
Simplify PyTorch model training with Lightning's LightningModule for automatic GPU and logging.
Best for: deep learning training
#pytorch-lightning#training