-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathdata_utils.py
More file actions
179 lines (137 loc) · 4.1 KB
/
data_utils.py
File metadata and controls
179 lines (137 loc) · 4.1 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Time:
2021-08-23 7:28 下午
Author:
huayang
Subject:
"""
import doctest
import warnings
import numpy as np
__all__ = [
'safe_indexing',
'split',
]
def shuffle(rows, random_seed=None):
""""""
rs = np.random.RandomState(random_seed)
rs.shuffle(rows) # in-place
return rows
def safe_indexing(x, indices=None):
"""
Return items or rows from X using indices.
Args:
x:
indices:
References:
sklearn.utils.safe_indexing
"""
if indices is None:
return x
if hasattr(x, "shape"): # for numpy
try:
return x.take(indices, axis=0) # faster
except: # noqa
return x[indices]
elif hasattr(x, "iloc"): # for pandas
indices = np.asarray(indices)
indices = indices if indices.flags.writeable else indices.copy()
try:
return x.iloc[indices]
except: # noqa
return x.copy().iloc[indices]
else: # for python
return [x[idx] for idx in indices]
def split(*arrays, split_size=0.2, random_seed=1, shuffle=True):
"""@NLP Utils
将数据按比例切分
Args:
*arrays:
split_size: 切分比例,采用向上取整:ceil(6*0.3) = 2
random_seed: 随机数种子
shuffle: 是否打乱
Examples:
>>> data = [[0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7]]
>>> xt, xv = split(*data, split_size=0.3, shuffle=False)
>>> xt
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
>>> xv
[[5, 6, 7], [5, 6, 7], [5, 6, 7]]
Returns:
x_train, x_val = split(x)
(a_train, b_train, c_train), (a_val, b_train, c_train) = split(a, b, c)
"""
# assert
lens = [len(x) for x in arrays]
if len(set(lens)) > 1:
raise ValueError('The length of each array must be same, but %r.' % lens)
n_sample = lens[0]
if shuffle:
rs = np.random.RandomState(random_seed)
idx = rs.permutation(n_sample)
else:
idx = np.arange(n_sample)
n_val = int(np.ceil(split_size * n_sample))
arr_train = [safe_indexing(x, idx[:-n_val]) for x in arrays]
arr_val = [safe_indexing(x, idx[-n_val:]) for x in arrays]
if len(arrays) == 1:
return arr_train[0], arr_val[0]
return arr_train, arr_val
def simple_split(*arrays, val_size=0.25, random_seed=1, shuffle=True):
"""
将数据按比例切分
Args:
*arrays:
val_size: 切分比例
random_seed: 随机数种子
shuffle: 是否打乱
Examples:
(a_train, b_train, c_train), (a_val, b_train, c_train) = split(a, b, c)
"""
warnings.warn(f"Recommend using `{split.__name__}` instead", DeprecationWarning)
# assert
assert all(len(arrays[0]) == len(arr) for arr in arrays), "Size mismatch between tensors"
if shuffle:
rs = np.random.RandomState(random_seed)
tmp = list(zip(*arrays))
rs.shuffle(tmp)
arrays = list(zip(*tmp))
n_sample = len(arrays[0])
n_val = int(np.ceil(val_size * n_sample))
arr_val = [x[-n_val:] for x in arrays]
arr_train = [x[:-n_val] for x in arrays]
return arr_train, arr_val
def unzip(data):
"""
unzip([[1,2,3], [1,2,3], ..]) -> [[1,1,..], [2,2,..], [3,3,..]]
"""
try:
out_type = type(data)
in_type = type(data[0])
return out_type([in_type(it) for it in zip(*data)]) # 尝试还原类型,不一定都能成功
except: # noqa
return zip(*data)
def _test():
""""""
doctest.testmod()
def _test_split():
""""""
data = [[i for i in range(10)]] * 3
data_a, data_b = simple_split(*data, shuffle=False)
print(data_a)
print(data_b)
_test_split()
def _test_unzip():
""""""
from sortedcollections import SegmentList
data = SegmentList([[1, 2, 3], (1, 2, 3)])
tmp = unzip(data)
print(tmp)
# for it in tmp:
# print(it)
_test_unzip()
if __name__ == '__main__':
""""""
_test()