Skip to main content
skill CX Codex

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.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add Mindrally/skills --skill scikit-learn-best-practices -a claude-code
Open on GitHub

What 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 Pipeline without leakage
  • pick a metric that survives class imbalance instead of accuracy
  • tune hyperparameters with GridSearchCV or RandomizedSearchCV
  • persist a fitted pipeline to disk with joblib

Example prompts for the agent

  • Build a pipeline with StandardScaler and RandomForestClassifier, then check it with cross-validation
  • Why is accuracy the wrong metric here? Pick one for imbalanced classes
  • Save the fitted pipeline with joblib and 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-code

2Manually: put the files in place

~/.claude/skills/scikit-learn-best-practices/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/scikit-learn-best-practices/

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

Similar skills

Everything for AI and ML →

From the same set214

design-systems Walks your agent through design tokens, a component library and accessibility rules in one project. CX Codex +1 903installs wordpress The wordpress skill gives an agent a rulebook for WordPress and WooCommerce work in PHP. It keeps code inside WordPress standards and reminds the agent about hooks, nonces and prepared queries. CX Codex +1 894installs mongodb-development MongoDB development rules: schemas, indexes, aggregation pipelines, Mongoose and Payload CMS. Useful when the agent writes a TypeScript backend. CX Codex +1 891installs pandas-best-practices Guides the AI assistant to write idiomatic Pandas code using vectorization, method chaining, and proper data types. Useful for data analysis and refactoring slow DataFrame transformations. CX Codex +1 886installs astro A skill that tells the agent how to build Astro sites: static generation, partial hydration, file-based routing and as little client-side JavaScript as possible. CX Codex +1 886installs beautifulsoup-parsing Teaches AI agents to parse HTML and XML with BeautifulSoup in Python. Helps write cleaner scrapers with safe data extraction and DOM navigation. CX Codex +1 877installs bash-scripting This skill gives the agent rules for writing Bash scripts: input validation, error handling, portability. Useful when the agent writes deploy, backup or cron tasks. CX Codex +1 875installs swr SWR data-fetching guidelines for React and TypeScript apps. It guides agents on caching, request deduplication, and optimistic mutations. CX Codex +1 870installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor