Skip to content
v0.1.3latest release121 skills4 categories6 sub-categories247 tested scripts397 reference docsContact us

cv_advisor.py

Helper script for cross-validation-strategy-designer · 174 lines

AI & Machine Learning / Classical Machine Learningv1.0.0Updated OCT 25 20263 references2 scripts
scripts/cv_advisor.py
#!/usr/bin/env python3
"""Recommend a cross-validation scheme from the data itself, with the scikit-learn code.

It inspects the CSV for the three things that decide a CV design:
  repeated entities   columns whose values repeat across rows (customer_id, patient, store) -> grouped folds
                      (--group-col, or auto-detected ID-like columns with many repeats)
  time order          a datetime column (--time-col or auto-detected) -> forward-chaining folds,
                      with a gap of --label-window rows/days when labels look ahead
  target              binary / multiclass / numeric, imbalance and minority count -> stratification,
                      number of folds and repeats
and the deployment question (--deploy forward|new-entities|same-entities|iid) when given.

Output: the recommended splitter, folds/repeats, gap, nested-CV advice when tuning, a warning list
(e.g. "random K-fold on data with repeated customers"), and copy-paste code.

Usage:
    python cv_advisor.py data.csv --target churned
    python cv_advisor.py snapshots.csv --target churned --group-col customer_id --time-col score_date --label-window 30
    python cv_advisor.py data.csv --target price --deploy iid --tuning --json

Exit codes: 0 printed, 2 usage or read error.
"""

import argparse
import csv
import json
import math
import re
import sys
from collections import Counter

MISSING = {"", "na", "n/a", "nan", "null", "none", "?"}
DATE_RE = re.compile(r"^\d{4}[-/]\d{1,2}[-/]\d{1,2}")


def is_num(v):
    try:
        return math.isfinite(float(v))
    except ValueError:
        return False


def detect(header, rows, target):
    groups, times = [], []
    n = len(rows)
    for c in header:
        if c == target:
            continue
        vals = [(r[c] or "").strip() for r in rows if (r[c] or "").strip().lower() not in MISSING]
        if not vals:
            continue
        if sum(bool(DATE_RE.match(v)) for v in vals) / len(vals) > 0.9:
            times.append(c)
            continue
        k = len(set(vals))
        if re.search(r"(_id$|^id_|^id$|user|customer|patient|account|store|device|session|card|subject)", c, re.I) \
                and 1 < k < 0.8 * n:
            groups.append((c, n / k))
    return sorted(groups, key=lambda g: -g[1]), times


def main():
    ap = argparse.ArgumentParser(description="Recommend a CV scheme and emit scikit-learn code.", epilog=__doc__,
                                 formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("csv")
    ap.add_argument("--target", required=True)
    ap.add_argument("--group-col")
    ap.add_argument("--time-col")
    ap.add_argument("--label-window", type=int, default=0, help="label look-ahead in rows (gap between train and test)")
    ap.add_argument("--deploy", choices=["auto", "forward", "new-entities", "same-entities", "iid"], default="auto")
    ap.add_argument("--tuning", action="store_true", help="hyperparameters will be tuned (nested CV advice)")
    ap.add_argument("--json", action="store_true")
    a = ap.parse_args()
    try:
        with open(a.csv, newline="", encoding="utf-8-sig") as fh:
            rd = csv.DictReader(fh)
            header = rd.fieldnames or []
            rows = list(rd)
    except OSError as e:
        print(f"error: {e}", file=sys.stderr)
        return 2
    for c in (a.target, a.group_col, a.time_col):
        if c and c not in header:
            print(f"error: column {c!r} not found", file=sys.stderr)
            return 2
    n = len(rows)
    groups, times = detect(header, rows, a.target)
    group = a.group_col or (groups[0][0] if groups and groups[0][1] >= 1.5 else None)
    tcol = a.time_col or (times[0] if times else None)
    tv = [(r[a.target] or "").strip() for r in rows if (r[a.target] or "").strip()]
    cnt = Counter(tv)
    classification = len(cnt) <= 20 or not all(is_num(v) for v in tv)
    minority = min(cnt.values()) if classification else None
    deploy = a.deploy
    if deploy == "auto":
        deploy = "forward" if a.time_col else ("new-entities" if group else "iid")
    warnings = []
    if group and deploy not in ("new-entities", "same-entities", "forward"):
        warnings.append(f"{group} repeats ({n / len(set(r[group] for r in rows)):.1f} rows per value): random K-fold "
                        "would put the same entity in train and test and inflate scores")
    if tcol and deploy != "forward":
        warnings.append(f"{tcol} looks like a time column: if the model predicts later periods, use forward-chaining "
                        f"folds (--time-col {tcol} or --deploy forward); random or grouped folds mix past and future")
    if deploy == "forward" and not tcol:
        warnings.append("forward deployment but no time column found: give --time-col")
    if classification and minority is not None and minority < 50:
        warnings.append(f"only {minority} rows in the smallest class: use fewer folds, repeats and report intervals")
    # choose
    folds = 5
    repeats = 1
    if n < 1000:
        repeats = 5 if deploy != "forward" else 1
        folds = 5 if (minority or 100) >= 25 else 3
    elif n > 1_000_000:
        folds = 3
    if deploy == "forward":
        splitter = f"TimeSeriesSplit(n_splits={folds}, gap={a.label_window})"
        why = "the model predicts later periods: train on the past, test on the next block"
        code = (f"df = df.sort_values({tcol!r}).reset_index(drop=True)\n"
                f"cv = TimeSeriesSplit(n_splits={folds}, gap={a.label_window})   # gap = label window in rows\n"
                "for tr, te in cv.split(df):\n    ...")
        if group:
            why += f"; {group} also repeats: entities appear across time, which matches deployment (same entities, later "  \
                   "time) - if new entities must be predicted, also exclude test entities from training"
        if a.label_window == 0:
            warnings.append("labels with a look-ahead window (e.g. churn in 30 days) need gap >= window, otherwise the "
                            "last training rows' labels overlap the test period")
    elif deploy == "new-entities":
        if classification:
            splitter = f"StratifiedGroupKFold(n_splits={folds}, shuffle=True, random_state=0)"
        else:
            splitter = f"GroupKFold(n_splits={folds})"
        why = f"production scores entities never seen in training: every {group} is in one fold only"
        code = f"cv = {splitter}\nscores = cross_val_score(pipe, X, y, cv=cv, groups=df[{group!r}])"
    else:
        if classification:
            splitter = (f"RepeatedStratifiedKFold(n_splits={folds}, n_repeats={repeats}, random_state=0)" if repeats > 1
                        else f"StratifiedKFold(n_splits={folds}, shuffle=True, random_state=0)")
            why = "i.i.d. rows: stratify so every fold keeps the class balance"
        else:
            splitter = (f"RepeatedKFold(n_splits={folds}, n_repeats={repeats}, random_state=0)" if repeats > 1
                        else f"KFold(n_splits={folds}, shuffle=True, random_state=0)")
            why = "i.i.d. rows, numeric target"
        if deploy == "same-entities" and group:
            why += f"; {group} repeats but production scores the same entities again, so mixing is acceptable"
        code = f"cv = {splitter}\nscores = cross_val_score(pipe, X, y, cv=cv)"
    nested = None
    if a.tuning:
        nested = ("tune inside each outer training fold: cross_validate(GridSearchCV(pipe, grid, cv=inner), X, y, cv=outer); "
                  "inner and outer splitters must respect the same groups / time order")
    out = {"rows": n, "target_kind": "classification" if classification else "regression", "minority_count": minority,
           "detected_groups": [g for g, _ in groups], "detected_time_columns": times, "group_col": group,
           "time_col": tcol, "deploy": deploy, "splitter": splitter, "folds": folds, "repeats": repeats,
           "why": why, "nested": nested, "warnings": warnings, "code": code}
    if a.json:
        print(json.dumps(out, indent=2))
        return 0
    print(f"{n} rows, {out['target_kind']} target" + (f" (smallest class {minority})" if minority else "") +
          f"; repeated-entity columns: {', '.join(g for g, _ in groups) or 'none'}; time columns: {', '.join(times) or 'none'}")
    print(f"deployment assumed: {deploy}")
    print(f"\nRECOMMENDED: {splitter}\n  why: {why}")
    if nested:
        print(f"  tuning: {nested}")
    for w in warnings:
        print(f"warning: {w}")
    print("\ncode:\nfrom sklearn.model_selection import (GroupKFold, KFold, RepeatedKFold, RepeatedStratifiedKFold,\n"
          "    StratifiedGroupKFold, StratifiedKFold, TimeSeriesSplit, cross_val_score)\n" + code)
    print("\nCheck the folds before trusting scores: python fold_preview.py ...")
    return 0


if __name__ == "__main__":
    sys.exit(main())
More in Classical Machine Learning

anomaly-detection-model-builder

Build and evaluate anomaly detection for tabular, sensor and transaction data with few or no labels: define the anomaly type (point, contextual, collective), choose detectors (robust z-score/MAD and IQR rules, Mahalanobis with robust covariance, k-NN distance, LOF, Isolation Forest, one-class SVM, PCA reconstruction / autoencoders, forecasting residuals for time series), combine them by rank, explain each alert by its deviating features, set thresholds from an alert budget or the score distribution, and evaluate with precision@k, PR-AUC and recall on known incidents, or detector agreement when no labels exist. Includes pure-Python detectors (robust z, Mahalanobis, kNN, Isolation Forest, ensemble) and an evaluation script.

catboost-model-builder

Build CatBoost models that use its strengths correctly: raw categorical columns as cat_features (ordered target statistics, no manual encoding), text_features, Pool construction with NaN-safe categories, ordered boosting and has_time for temporal data, symmetric (oblivious) trees, overfitting detector with eval_set and use_best_model, losses for classification, regression (Quantile, Poisson, Tweedie) and ranking (YetiRank), class weights, monotone constraints, feature importance (PredictionValuesChange, LossFunctionChange) and SHAP values, GPU training and model export. Plans cat/text/numeric columns from a CSV and generates a runnable training script, and checks parameter sets for the conflicts CatBoost rejects (iterations vs n_estimators, Bayesian bootstrap with subsample, multiple class-weight options, depth > 16, use_best_model without eval_set).

categorical-encoding-selector

Choose and implement the right encoding for every categorical column given the model family and the data: one-hot (with infrequent-level grouping and handle_unknown), ordinal with an explicit order, cross-fitted target encoding (scikit-learn TargetEncoder, CatBoost ordered statistics, category_encoders inside CV), frequency/count encoding, feature hashing, native categorical support in LightGBM, HistGradientBoosting, XGBoost and CatBoost, prefix levels for codes (zip3, NAICS-2), and embeddings. Measures cardinality, rare-level share, unseen-level rate on a time-ordered holdout and target signal per column, emits the ColumnTransformer, and quantifies target-encoding leakage (in-sample vs out-of-fold vs holdout AUC).

classification-model-selector

Choose the right classification algorithm for tabular, text or mixed data and prove the choice with a fair bake-off: logistic regression (L2, L1, elastic net), linear SVM, naive Bayes, k-NN, RBF SVM, decision trees, random forest, extra trees, HistGradientBoosting, LightGBM, XGBoost, CatBoost, explainable boosting machines (EBM/GAM), points scorecards and MLPs, matched to data size, width, categorical and high-cardinality share, missingness, class imbalance, text, and constraints such as interpretability, monotonic constraints, calibrated probabilities, latency budgets and tiny deployable models. Ranks candidates with starting hyperparameters and cautions, then generates a bake-off script running every candidate on identical CV folds with per-fold metrics, fit time and prediction latency.

clustering-algorithm-selector

Choose the right clustering algorithm and check that clustering makes sense at all: cluster tendency (Hopkins statistic with a correlated-features caveat, nearest-neighbour isolation), then k-means / MiniBatchKMeans, Gaussian mixtures with BIC, agglomerative (Ward, average, complete, single) with dendrograms, DBSCAN, HDBSCAN, OPTICS, spectral clustering, BIRCH, mean shift, k-prototypes and k-modes for mixed or categorical data, and Gower distance, matched to data size, expected shapes, noise, varying density, known k, soft memberships, hierarchy and streaming. Covers preprocessing (scaling, encoding, dimensionality, distance metrics), validation (internal indices, bootstrap stability, external labels, business profiling) and assigning new points.

collaborative-filtering-implementer

Implement collaborative filtering recommenders that beat popularity: item-based kNN with cosine, Jaccard or asymmetric similarity, shrinkage, neighbourhood size, positive-only or mean-centred rating signals and 'because you liked X' explanations; implicit ALS with confidence weighting, BPR pairwise ranking and the closed-form EASE model in plain NumPy, tuned on a temporal validation split by recall@k and NDCG@k and refit on all data; plus user-kNN, explicit matrix factorisation, the implicit/LightFM libraries, hyperparameter ranges, cold-start fallbacks, scaling with sparse matrices and approximate nearest neighbours. Produces top-k lists in the format of the recommendation evaluator.