Created
November 22, 2022 03:32
-
-
Save mikelkl/627165babac85079e1a1019ea1b6f7d3 to your computer and use it in GitHub Desktop.
[K-Folds split] #Python
This file contains hidden or 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
from typing import List | |
import pandas as pd | |
from sklearn.model_selection import StratifiedKFold | |
def split_k_fold(df: pd.DataFrame, k: int = 5) -> List[pd.DataFrame]: | |
skf = StratifiedKFold(n_splits=k) | |
y = df["label"] | |
placeholder = y.copy() | |
k_fold = [] | |
for _, test_idx in skf.split(placeholder, y): | |
df_fold = df.iloc[test_idx] | |
k_fold.append(df_fold) | |
return k_fold |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment