scikit-learn-best-practices — Claude Code skill: ML rules for Python
A skill that hands your agent scikit-learn conventions: pipelines, cross-validation, metrics, model persistence. Useful when you write ML code in Python.
npx skills add Mindrally/skills --skill scikit-learn-best-practices -a claude-codeWhat scikit-learn-best-practices does
The skill lays out how to run a scikit-learn project: split data into train/validation/test first, engineer features next, chain everything into pipelines, and touch the held-out set once at the end. Your agent picks up those rules and applies them while writing code, instead of fitting a model on the whole dataset.
It covers data preparation, feature engineering, hyperparameter tuning, classification and regression metrics, imbalanced classes, and saving models. Scaling advice is split by distribution: StandardScaler, MinMaxScaler, RobustScaler. Categorical variables go through OneHotEncoder, OrdinalEncoder or LabelEncoder. Pipeline and ColumnTransformer are presented as the way to avoid data leakage, since transformers fit on training data only. Models are meant to be saved with joblib as full pipelines.
There is nothing executable here. The skill is text, it installs no dependencies, and it assumes scikit-learn is already in your project. Code samples target Python. On a different ML stack it does nothing for you.
What it helps with
- split data before preprocessing and keep runs reproducible
- chain a scaler and a model in a
Pipelinewithout leakage - pick a metric that survives class imbalance instead of accuracy
- tune hyperparameters with
GridSearchCVorRandomizedSearchCV - persist a fitted pipeline to disk with
joblib
Example prompts for the agent
- Build a pipeline with
StandardScalerandRandomForestClassifier, then check it with cross-validation - Why is accuracy the wrong metric here? Pick one for imbalanced classes
- Save the fitted pipeline with
jobliband list the hyperparameters you used
How to install scikit-learn-best-practices
1Skills CLI (Vercel)
npx skills add Mindrally/skills --skill scikit-learn-best-practices -a claude-code2Manually: put the files in place
~/.claude/skills/scikit-learn-best-practices/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/scikit-learn-best-practices/
1Skills CLI (Vercel)
npx skills add Mindrally/skills --skill scikit-learn-best-practices -a codex2Manually: put the files in place
.agents/skills/scikit-learn-best-practices/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add Mindrally/skills --skill scikit-learn-best-practices -a cursor2Manually: put the files in place
.cursor/skills/scikit-learn-best-practices/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install Mindrally/skills scikit-learn-best-practices2Manually: put the files in place
.github/skills/scikit-learn-best-practices/SKILL.md1Skills CLI (Vercel)
npx skills add Mindrally/skills --skill scikit-learn-best-practices -a gemini-cli2Platform CLI
gemini skills install https://github.com/Mindrally/skills --path scikit-learn-best-practicesManually: put the files in place
.devin/skills/scikit-learn-best-practices/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add Mindrally/skills --skill scikit-learn-best-practices -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about scikit-learn-best-practices
How do I install scikit-learn-best-practices in Claude?
Run npx skills add Mindrally/skills --skill scikit-learn-best-practices -a claude-code. The skill lands in ~/.claude/skills/scikit-learn-best-practices/SKILL.md.
What does scikit-learn-best-practices do?
It gives the agent a set of scikit-learn rules: how to split data, build pipelines, evaluate models and save them. It acts as a reference while you write ML code.
Does the skill require extra libraries?
The skill installs nothing itself. Its examples are Python and assume scikit-learn is present, and joblib is named for saving models.
Source text
SKILL.md · Apache-2.0
---
name: scikit-learn-best-practices
description: Best practices for scikit-learn machine learning, model development, evaluation, and deployment in Python
metadata:
maintainer: Mindrally
source: https://github.com/Mindrally/skills
---
# Scikit-learn Best Practices
Expert guidelines for scikit-learn development, focusing on machine learning workflows, model development, evaluation, and best practices.
## Code Style and Structure
- Write concise, technical responses with accurate Python examples
- Prioritize reproducibility in machine learning workflows
- Use functional programming for data pipelines
- Use object-oriented programming for custom estimators
- Prefer vectorized operations over explicit loops
- Follow PEP 8 style guidelines
## Machine Learning Workflow
### Data Preparation
- Always split data before any preprocessing: train/validation/test
- Use `train_test_split()` with `random_state` for reproducibility
- Stratify splits for imbalanced classification: `stratify=y`
- Keep test set completely separate until final evaluation
### Feature Engineering
- Scale features appropriately for distance-based algorithms
- Use `StandardScaler` for normally distributed features
- Use `MinMaxScaler` for bounded features
- Use `RobustScaler` for data with outliers
- Encode categorical variables: `OneHotEncoder`, `OrdinalEncoder`, `LabelEncoder`
- Handle missing values: `SimpleImputer`, `KNNImputer`
### Pipelines
- Always use `Pipeline` to chain preprocessing and modeling
- Prevents data leakage by fitting transformers only on training data
- Makes code cleaner and more reproducible
- Enables easy deployment and serialization
```python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
pipeline = Pipeline([
('scaler', StandardScaler()),
('classifier', RandomForestClassifier(random_state=42))
])
```
### Column Transformers
- Use `ColumnTransformer` for different preprocessing per feature type
- Combine numeric and categorical preprocessing in single pipeline
## Model Selection and Tuning
### Cross-Validation
- Use cross-validation for reliable performance estimates
- `cross_val_score()` for quick evaluation
- `cross_validate()` for multiple metrics
- Use appropriate CV strategy:
- `KFold` for regression
- `StratifiedKFold` for classification
- `TimeSeriesSplit` for temporal data
- `GroupKFold` for grouped data
### Hyperparameter Tuning
- Use `GridSearchCV` for exhaustive search
- Use `RandomizedSearchCV` for large parameter spaces
- Always tune on training/validation data, never test data
- Set `n_jobs=-1` for parallel processing
## Model Evaluation
### Classification Metrics
- Use appropriate metrics for your problem:
- `accuracy_score` for balanced classes
- `precision_score`, `recall_score`, `f1_score` for imbalanced
- `roc_auc_score` for ranking ability
- Use `classification_report()` for comprehensive overview
- Examine `confusion_matrix()` for error analysis
### Regression Metrics
- `mean_squared_error` (MSE) for general use
- `mean_absolute_error` (MAE) for interpretability
- `r2_score` for explained variance
### Evaluation Best Practices
- Report confidence intervals, not just point estimates
- Use multiple metrics to understand model behavior
- Compare against meaningful baselines
- Evaluate on held-out test set only once, at the end
## Handling Imbalanced Data
- Use stratified splitting and cross-validation
- Consider class weights: `class_weight='balanced'`
- Use appropriate metrics (F1, AUC-PR, not accuracy)
- Adjust decision threshold based on business needs
## Feature Selection
- Use `SelectKBest` with statistical tests
- Use `RFE` (Recursive Feature Elimination)
- Use model-based selection: `SelectFromModel`
- Examine feature importances from tree-based models
## Model Persistence
- Use `joblib` for saving and loading models
- Save entire pipelines, not just models
- Version control model artifacts
- Document model metadata
## Performance Optimization
- Use `n_jobs=-1` for parallel processing where available
- Consider `warm_start=True` for iterative training
- Use sparse matrices for high-dimensional sparse data
- Consider incremental learning with `partial_fit()` for large data
## Key Conventions
- Import from submodules: `from sklearn.ensemble import RandomForestClassifier`
- Set `random_state` for reproducibility
- Use pipelines to prevent data leakage
- Document model choices and hyperparameters
© Mindrally · Apache-2.0 · Mindrally/skills