A Python library for specialized cross-validation strategies (stratified, time-series, spatial).
Project description
Custom CV: A Python Library for Specialized Cross-Validation
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file custom_cv-0.1.2.tar.gz.
File metadata
- Download URL: custom_cv-0.1.2.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
295d7874a38e51965e14d2562e3fbb5285c8df06794612f79e508a2f60b88f5a
|
|
| MD5 |
f450d3c17d52858a5f75a52e9e4aefaf
|
|
| BLAKE2b-256 |
3501127f4f4ef1a75d131a4281de5d798caeac976ef7cfd8bd1fdd4b0b4c02a5
|
File details
Details for the file custom_cv-0.1.2-py3-none-any.whl.
File metadata
- Download URL: custom_cv-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a72a716013f5c7ff1e12f9d824dd9a6907c83548934fd8d4dd626894a54776de
|
|
| MD5 |
c5491c61cae363d3dbff948241529691
|
|
| BLAKE2b-256 |
f52ae8181450e1830c88e4687eb2c3b5bfd27c0e0d7ac2c13bbf4a2e9fe168f5
|