forked from pdima/kaggle_RSNA_Pneumonia_Detection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit_to_folds.py
23 lines (17 loc) · 838 Bytes
/
split_to_folds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import pandas as pd
from sklearn.model_selection import StratifiedKFold
nb_folds = 4
training_samples = pd.read_csv('../input/stage_1_detailed_class_info.csv')
training_samples = training_samples.drop_duplicates().reset_index(drop=True)
X = training_samples['patientId']
y = training_samples['class']
training_samples['fold'] = -1
skf = StratifiedKFold(n_splits=nb_folds, shuffle=True, random_state=42)
for fold, (train_index, test_index) in enumerate(skf.split(X, y)):
training_samples.loc[test_index, 'fold'] = fold
training_samples.to_csv('../input/folds.csv', index=False)
for cls in training_samples['class'].unique():
print(cls)
cls_samples = training_samples[training_samples['class'] == cls].reset_index(drop=True)
for fold in range(nb_folds):
print(fold, len(cls_samples[cls_samples.fold == fold]))