<< All versions
Skill v1.0.0
currentLLM-judged scan98/100fuyu12345/terminalgen-skills/cross-validation-sklearn-medium
──Details
PublishedOctober 23, 2026 at 03:22 PM
Content Hashsha256:a25ef8c383c0c541...
Git SHA750b6184ec63
──Files
Files (1 file, 3.9 KB)
SKILL.md3.9 KBactive
SKILL.md · 96 lines · 3.9 KB
version: "1.0.0" name: evaluating-sklearn-models-with-cross-validation description: Build a reproducible sklearn cross-validation pipeline for mixed-type tabular data, then emit summary metrics JSON. Use when a task requires replacing unstable train/test evaluation with k-fold results.
Evaluating sklearn Models with Cross-Validation
When to Use
- Evaluate multiple classifiers on a tabular dataset with both numeric and categorical columns.
- Require robust metrics (mean/std across folds) instead of a single split.
- Need deterministic behavior (
random_state=42) and strict JSON output for grading/automation. - Need a terminal-safe, one-shot script that writes results to a required path.
Minimal Reliable Workflow
- Load data and split target
- Read CSV with pandas.
- Set
X = df.drop(columns=[target_col]),y = df[target_col].
- Detect feature types
- Build
categorical_colsfromobject/category/bool. - Treat remaining columns as numeric.
- Build preprocessing inside a pipeline
- Use
ColumnTransformerwith: - categorical:
OneHotEncoder(handle_unknown='ignore') - numeric: passthrough (or optional imputation if required).
- Keep preprocessing inside sklearn
Pipelineto avoid leakage during CV.
- Define CV strategy for reproducibility
- Use
StratifiedKFold(n_splits=5, shuffle=True, random_state=42)for binary classification.
- Evaluate each model with identical CV
- Wrap each model in
Pipeline([('preprocess', ...), ('model', ...)]). - Run
cross_val_score(..., scoring='accuracy'). - Compute and round:
mean_accuracy = round(scores.mean(), 4)std_accuracy = round(scores.std(), 4)
- Write strict JSON schema
- Save to required location (create parent dir first).
- Use stable keys expected by tests, e.g.:
logistic_regressionrandom_forestgradient_boosting
- Confirm file content before completion
catthe JSON and sanity-check keys and value ranges.
Common Pitfalls
- Marking completion before command output flushes
- In two runs, heredoc execution looked incomplete until waiting briefly; verification commands appeared delayed.
- Prevent by polling (
""wait command) and confirming prompt/output before finalizing.
- Command concatenation from missing newline
- One run emitted a warning: a command without terminal newline can concatenate with the next command.
- Prevent by ending every command string with
\n.
- Preprocessing outside CV pipeline
- Causes leakage and inflated metrics.
- Prevent by embedding
ColumnTransformerin per-modelPipeline.
- Non-reproducible splits/models
- Omitting
shuffle=True+random_statein CV (and model random states where applicable) makes run-to-run numbers unstable.
- Output schema drift
- Tests expect exact top-level model keys and metric field names; extra nesting or renamed fields can fail validation.
Verification Strategy
Use a two-layer verification aligned to observed test expectations:
- Artifact checks
- Confirm file exists at required path.
- Confirm valid JSON parse.
- Schema + metric checks
- Assert top-level keys for all required models.
- Assert each model has
mean_accuracyandstd_accuracy. - Assert
0 <= mean_accuracy <= 1. - Assert
std_accuracy >= 0. - Assert rounding/precision to 4 decimals in serialized values.
- Methodology checks
- Confirm 5-fold stratified CV with fixed random state.
- Confirm categorical handling via encoder in pipeline.
- Terminal reliability checks
- Wait for heredoc completion if output is pending.
- Re-run
catand (optionally) a short Python assert block before final submission.
References to Load On Demand
sklearn.pipeline.Pipelinesklearn.compose.ColumnTransformersklearn.model_selection.StratifiedKFold,cross_val_scoresklearn.preprocessing.OneHotEncoder