-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_split.py
executable file
·65 lines (59 loc) · 1.82 KB
/
data_split.py
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import json
import random
from data_prepare import write_data
import config
# Read data
def read_data(path):
data = []
with open(path, "r") as f:
lines = f.readlines()
for line in lines:
dic = json.loads(line)
data.append(dic)
print("data_length:" + str(len(data)))
return data
def split_data(data, train_ratio, valid_ratio, random_seed):
"""Splits data into train, validation and test according to ratio."""
train_data = []
valid_data = []
test_data = []
num_dic = {}
for label in config.labels:
num_dic[label] = 0
for item in data:
for i in num_dic:
if item[config.LABEL_NAME] == i:
num_dic[i] += 1
train_num_dic = {}
valid_num_dic = {}
for i in num_dic:
train_num_dic[i] = int(train_ratio * num_dic[i])
valid_num_dic[i] = int(valid_ratio * num_dic[i])
print(num_dic)
random.seed(random_seed)
random.shuffle(data)
for item in data:
for i in num_dic:
if item[config.LABEL_NAME] == i:
if train_num_dic[i] > 0:
train_data.append(item)
train_num_dic[i] -= 1
elif valid_num_dic[i] > 0:
valid_data.append(item)
valid_num_dic[i] -= 1
else:
test_data.append(item)
print("train_length:" + str(len(train_data)))
print("test_length:" + str(len(test_data)))
return train_data, valid_data, test_data
if __name__ == "__main__":
data = read_data("../../data/dataset")
train_data, valid_data, test_data = \
split_data(data, train_ratio=config.train_ratio, \
valid_ratio=config.valid_ratio, random_seed=config.data_split_random_seed)
write_data(train_data, "../../data/train")
write_data(valid_data, "../../data/valid")
write_data(test_data, "../../data/test")