-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstratified.py
More file actions
39 lines (33 loc) · 1.67 KB
/
stratified.py
File metadata and controls
39 lines (33 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#https://datascience.stackexchange.com/questions/45174/how-to-use-sklearn-train-test-split-to-stratify-data-for-multi-label-classificat/108575#108575
from iterstrat.ml_stratifiers import MultilabelStratifiedShuffleSplit
from sklearn.utils import indexable, _safe_indexing
from sklearn.utils.validation import _num_samples
from sklearn.model_selection._split import _validate_shuffle_split
from itertools import chain
def multilabel_train_test_split(*arrays,
test_size=None,
train_size=None,
random_state=None,
shuffle=True,
stratify=None):
"""
Train test split for multilabel classification. Uses the algorithm from:
'Sechidis K., Tsoumakas G., Vlahavas I. (2011) On the Stratification of Multi-Label Data'.
"""
if stratify is None:
return train_test_split(*arrays, test_size=test_size,train_size=train_size,
random_state=random_state, stratify=None, shuffle=shuffle)
assert shuffle, "Stratified train/test split is not implemented for shuffle=False"
n_arrays = len(arrays)
arrays = indexable(*arrays)
n_samples = _num_samples(arrays[0])
n_train, n_test = _validate_shuffle_split(
n_samples, test_size, train_size, default_test_size=0.25
)
cv = MultilabelStratifiedShuffleSplit(test_size=n_test, train_size=n_train, random_state=123)
train, test = next(cv.split(X=arrays[0], y=stratify))
return list(
chain.from_iterable(
(_safe_indexing(a, train), _safe_indexing(a, test)) for a in arrays
)
)