Created
January 19, 2021 11:30
-
-
Save adimyth/732e8affc543bc2c71d1a5b268edd28d to your computer and use it in GitHub Desktop.
StratifiedKFold Split for Regression Task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from sklearn.model_selection import StratifiedKFold | |
df = pd.read_csv(path_to_data) | |
n_bins = 1+np.log2(df.shape[0]) # Sturge's rule | |
df["bins"] = pd.cut(df.target, n_bins, labels=False) | |
n_folds = 5 | |
skf = StratifiedKFold(n_splits=n_folds) | |
df["fold"] = -1 | |
for fold, (train_idx, valid_idx) in enumerate(skf.split(df.bins, df.bins)): | |
df.loc[valid_idx, "fold"] = fold |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment