N Prompts for Machine Learning: Scikit-learn, XGBoost, CatBoost

Machine learning (ML) has moved from research labs to production pipelines in every industry. Yet many practitioners spend hours on repetitive tasks: data cleaning, hyperparameter tuning, model evaluation. The right prompts for AI assistants can slash that time from hours to minutes. This article provides a curated set of copy-paste-ready prompts for three core libraries—Scikit-learn (v1.6+), XGBoost (v2.1+), and CatBoost (v1.2+)—that cover the entire ML workflow: preprocessing, feature engineering, training, evaluation, and interpretation.

Each prompt is designed to be pasted directly into a chat-based AI assistant (like Claude, GPT-4, or Gemini). I explain what task it solves and show a concrete usage example. These prompts assume you have a dataset loaded as a pandas DataFrame named df with a target column named target.

1. Data Preprocessing with Scikit-learn

Prompt 1: Automatic Pipeline for Numeric and Categorical Features

"You are an ML preprocessing expert. Given a pandas DataFrame df with mixed data types, write Python code that:
- Separates numeric and categorical columns (excluding the target column target).
- Creates a Scikit-learn ColumnTransformer with:
- StandardScaler for numeric columns.
- OneHotEncoder (handle_unknown='ignore') for categorical columns.
- Wraps the transformer in a Pipeline that ends with a placeholder estimator (e.g., RandomForestClassifier).
- Prints the transformed shape and column names after fit_transform.
Assume the target is binary classification."

Why this prompt works: It forces the AI to produce production-ready code, handle missing data types gracefully, and show the output shape—critical for debugging.

Example usage: Paste the prompt after loading your data. The AI will output code like:

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier

numeric_cols = df.select_dtypes(include=['int64', 'float64']).columns.drop('target')
categorical_cols = df.select_dtypes(include=['object', 'category']).columns

preprocessor = ColumnTransformer(
    transformers=[
        ('num', StandardScaler(), numeric_cols),
        ('cat', OneHotEncoder(handle_unknown='ignore'), categorical_cols)
    ])

pipeline = Pipeline(steps=[
    ('preprocessor', preprocessor),
    ('classifier', RandomForestClassifier())
])

X_transformed = pipeline.named_steps['preprocessor'].fit_transform(df.drop('target', axis=1))
print(f"Transformed shape: {X_transformed.shape}")

Prompt 2: Handling Missing Values with Iterative Imputer

"Generate code to impute missing values in df using IterativeImputer from Scikit-learn. Use a RandomForestRegressor as the estimator. Only impute numeric columns. Set max_iter=10 and random_state=42. Print the number of missing values before and after imputation."

Why this works: IterativeImputer is often overlooked but outperforms simple mean/median imputation. This prompt forces the AI to import the experimental module (from sklearn.experimental import enable_iterative_imputer).

Example output snippet:

from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
from sklearn.ensemble import RandomForestRegressor

numeric_cols = df.select_dtypes(include=['int64', 'float64']).columns
print("Missing before:", df[numeric_cols].isnull().sum().sum())

imputer = IterativeImputer(estimator=RandomForestRegressor(), max_iter=10, random_state=42)
df_imputed = df.copy()
df_imputed[numeric_cols] = imputer.fit_transform(df[numeric_cols])
print("Missing after:", df_imputed[numeric_cols].isnull().sum().sum())

2. Feature Engineering with Scikit-learn

Prompt 3: Polynomial Features with Interaction Only

"Write code to add polynomial features of degree 2 to df using PolynomialFeatures from Scikit-learn. Set interaction_only=True and include_bias=False. Only apply to numeric columns. Print the number of original features vs. new features. Combine with the original DataFrame."

Why this works: Interaction features often capture non-linear relationships without exploding the feature space. The prompt specifies interaction_only to avoid quadratic terms.

Prompt 4: Custom Feature Selector Using Mutual Information

"Using SelectKBest with mutual_info_classif (for classification) or mutual_info_regression (for regression), select the top 20 features from df. Assume target is target. Print the selected feature names and their mutual information scores. Use random_state=42."

Why this works: Mutual information captures non-linear dependencies, unlike correlation. The prompt lets the AI decide the scoring function based on context.

3. Training Models with XGBoost

Prompt 5: XGBoost with Early Stopping and Custom Evaluation Metric

"Train an XGBoost classifier on df with target target. Perform a train-test split (80/20, random_state=42). Use XGBClassifier with n_estimators=1000, learning_rate=0.05, max_depth=6, subsample=0.8, colsample_bytree=0.8. Enable early stopping with early_stopping_rounds=20 using eval_set on the test set. Use eval_metric='logloss'. Print the best iteration and the test logloss. Also plot feature importance (type='weight')."

Why this works: Early stopping prevents overfitting. The prompt specifies hyperparameters that are known to work well on medium-sized tabular datasets.

Example code output:

import xgboost as xgb
from sklearn.model_selection import train_test_split

X = df.drop('target', axis=1)
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = xgb.XGBClassifier(
    n_estimators=1000,
    learning_rate=0.05,
    max_depth=6,
    subsample=0.8,
    colsample_bytree=0.8,
    early_stopping_rounds=20,
    eval_metric='logloss',
    random_state=42
)
model.fit(X_train, y_train, eval_set=[(X_test, y_test)], verbose=False)

print(f"Best iteration: {model.best_iteration}")
print(f"Test logloss: {model.evals_result()['validation_0']['logloss'][model.best_iteration]:.4f}")

xgb.plot_importance(model, importance_type='weight')

Prompt 6: XGBoost with Custom Objective Function

"Create a custom objective function for XGBoost that implements weighted binary cross-entropy, where positive class weight is 5 (to handle imbalance). Write the full training code with XGBClassifier using objective as a custom function. The function should take preds and dtrain as inputs. Use DMatrix for training. Evaluate with AUC."

Why this works: Imbalanced datasets are common. The custom objective prompt teaches how to go beyond built-in objectives.

4. Training Models with CatBoost

Prompt 7: CatBoost with Automatic Categorical Feature Handling

"Train a CatBoost classifier on df. Assume the target is target. Identify categorical columns automatically (dtype 'object' or 'category'). Use CatBoostClassifier with iterations=500, learning_rate=0.1, depth=6, auto_class_weights='Balanced'. Enable verbose logging every 50 iterations. Use cat_features parameter. Print the best score on a validation set (20% holdout)."

Why this works: CatBoost handles categorical features natively without one-hot encoding. The prompt forces the AI to use the cat_features parameter correctly.

Prompt 8: CatBoost with GPU Training and Feature Importance

"Modify the above CatBoost code to use GPU training by setting task_type='GPU' and devices='0'. After training, plot SHAP summary plot for the top 10 features. Use shap library. Print the training time in seconds."

Why this works: GPU acceleration is crucial for large datasets. SHAP plots provide model interpretability, which is a growing requirement in regulated industries.

5. Model Evaluation and Interpretation

Prompt 9: Comprehensive Classification Report with Threshold Tuning

"Write a function that takes true labels and predicted probabilities (for binary classification) and prints:
- ROC-AUC score with 95% confidence interval (using bootstrapping, n_iter=1000)
- Precision-Recall curve and AUC-PR
- Optimal threshold based on Youden's J statistic
- Confusion matrix at optimal threshold
- Classification report (precision, recall, f1) at optimal threshold
Use Scikit-learn and numpy."

Why this works: Most tutorials only report accuracy. This prompt gives a production-grade evaluation.

Prompt 10: SHAP Force Plot for a Single Prediction

"After training an XGBoost model, compute SHAP values for the test set. Select a single prediction (index 0) and display a SHAP force plot. Also print the base value and the model's predicted probability. Use the shap library."

Why this works: Explainability is key for stakeholder trust. The prompt ensures the output is visual and interpretable.

6. Putting It All Together: A Complete Pipeline

Prompt 11: End-to-End AutoML-Style Pipeline

"Create a Python script that:
1. Loads data from a CSV file path (ask user for path).
2. Automatically detects column types (numeric, categorical, target).
3. Splits into train/test (80/20).
4. Builds a Scikit-learn Pipeline with ColumnTransformer (StandardScaler + OneHotEncoder).
5. Trains three models: LogisticRegression, RandomForestClassifier, XGBClassifier.
6. Evaluates each on test set using ROC-AUC, accuracy, and F1.
7. Prints the best model name and its parameters.
8. Saves the best model using joblib.
Use GridSearchCV with 3-fold CV for RandomForest only (to limit runtime)."

Why this works: This is a realistic mini-project that many data scientists face daily. The prompt is specific enough to generate production-ready code.

7. Troubleshooting Common Issues

Prompt 12: Debugging XGBoost 'DMatrix' Type Errors

"I get the error 'Data type of feature column is not supported by DMatrix' when training XGBoost. My DataFrame has columns of type 'object' (strings). Write code to convert all object columns to category codes before creating DMatrix. Also check for NaN values and suggest imputation."

Why this works: This is a frequent pain point. The prompt solves a real problem and teaches best practices.

Conclusion

These 12 prompts cover the most common ML tasks with Scikit-learn, XGBoost, and CatBoost. By using them as templates, you can rapidly prototype models, tune hyperparameters, and interpret results without writing boilerplate from scratch. The key is to be specific: mention library versions, parameter names, and expected outputs. As of mid-2026, all three libraries are stable and widely used in production. For a deeper dive into integrating these models with external APIs and databases, ASI Biont supports connecting to various data sources through API — you can find more details on asibiont.com/courses. Remember to always validate the generated code on a sample of your data before full-scale training.

← All posts

Comments