Skip to main content

A Python library for specialized cross-validation strategies (stratified, time-series, spatial).

Project description

Custom CV: A Python Library for Specialized Cross-Validation

PyPI version License: MIT

Custom CV is a Python library that provides specialized cross-validation strategies for machine learning tasks where standard K-Fold cross-validation is insufficient. It includes robust implementations for stratified, time-series, and spatial data, along with tools for model comparison and evaluation.

This library is designed to be a simple, intuitive, and powerful tool for data scientists and machine learning practitioners who need to perform more rigorous model validation.

Key Features

  • Stratified K-Fold CV: Ensures class distribution is preserved in each fold, which is critical for imbalanced datasets.
  • Time-Series CV: Respects the temporal order of data, ensuring models are always tested on "future" data relative to the training set.
  • Spatial Block CV: Prevents data leakage from spatial autocorrelation by splitting data into geographic blocks.
  • Statistical Significance Testing: Includes a paired t-test to determine if the performance difference between two models is statistically significant.
  • Rich Visualizations: Tools to plot and compare model performance across folds, and to visualize how the data is split by each CV strategy.

Installation

You can install custom-cv directly from PyPI using pip:

pip install custom-cv

The package requires Python 3.8 or higher.

Quickstart Example

Here is a simple example of how to use TimeSeriesCV to generate splits and visualize them.

import numpy as np
import matplotlib.pyplot as plt
from custom_cv import TimeSeriesCV, plot_cv_splits

# 1. Create some dummy time-series data
n_samples = 50
X = np.arange(n_samples).reshape(-1, 1)
y = X.flatten() * 2 + np.random.randn(n_samples) * 5

# 2. Initialize our custom time-series splitter
ts_cv = TimeSeriesCV(n_splits=5, test_size=3, gap=2)

# 3. Generate and inspect the splits
print("--- TimeSeriesCV Splits ---")
for fold_num, (train_idx, test_idx) in enumerate(ts_cv.split(X)):
    print(f"Fold {fold_num+1}: Train size={len(train_idx)}, Test size={len(test_idx)}")
    # ... your model training and evaluation logic would go here ...

# 4. Visualize how the data was split
print("\nGenerating split visualization...")
fig, ax = plot_cv_splits(ts_cv, X, y)
plt.show() # In a script, or it will display automatically in a notebook

Model Comparison Example

You can also use custom-cv to compare models and test for statistical significance.

from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from custom_cv import StratifiedKFoldCV, plot_cv_scores, paired_ttest_cv

# Create data and models
X, y = make_classification(n_samples=100, n_classes=2, flip_y=0.05, random_state=42)
models = {"LR": LogisticRegression(), "RF": RandomForestClassifier(random_state=42)}
cv = StratifiedKFoldCV(n_splits=10)
results = {}

# Evaluate models
for name, model in models.items():
    scores = []
    for train, test in cv.split(X, y):
        model.fit(X[train], y[train])
        scores.append(accuracy_score(y[test], model.predict(X[test])))
    results[name] = scores

# Perform significance test and visualize
t_stat, p_value, is_sig = paired_ttest_cv(results["RF"], results["LR"])
print(f"P-value: {p_value:.4f}, Significant: {is_sig}")

fig, ax = plot_cv_scores(results)
plt.show()

Contributing

Contributions are welcome! If you'd like to contribute, please feel free to fork the repository and submit a pull request. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

custom_cv-0.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

custom_cv-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file custom_cv-0.1.0.tar.gz.

File metadata

  • Download URL: custom_cv-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.0

File hashes

Hashes for custom_cv-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4f2e076d6b24e1ddce3cee904b5de0ff2a977754b3da693a9908ccc2214f670d
MD5 5baf62943ae295ae7e8bd41cd6a8e65b
BLAKE2b-256 7b755b3c86a9505d9f5ea04a27d2a38d1e73a76d82ab59aff7d3c12de1235f02

See more details on using hashes here.

File details

Details for the file custom_cv-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: custom_cv-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.0

File hashes

Hashes for custom_cv-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea0f1b0ffe9a530af0039ca9c683cf94564dfa8e1765b85d9922106bd1ddb743
MD5 f4cbfc2f5a4cba4f57ab302b39348c95
BLAKE2b-256 6961bd3d4d2157baeae6ea98475cbadc3812e889cc870a935659160e43975bd8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page