-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_test.py
54 lines (42 loc) · 1.24 KB
/
train_test.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
from utils import *
from sklearn.cross_validation import StratifiedKFold
from random import seed, shuffle
def read_data(filename):
data = []
with open(filename, 'r') as f:
for line in f:
data.append(line)
seed(6)
shuffle(data)
return data
def extract_labels(data):
labels = []
for d in data:
tokens = d.split(' ')
labels.append(LABEL[tokens[0]])
return labels
def train_test_split(labels, data):
train, test = [], []
n_folds = 5 # for 80-20 split
skf = StratifiedKFold(y=labels, n_folds=n_folds, random_state=6)
for train_idx, test_idx in skf:
for idx in train_idx:
train.append(data[idx])
for idx in test_idx:
test.append(data[idx])
return train, test
def write_file(filename, data):
with open(filename, 'w') as f:
for line in data:
f.write(line)
def main():
infile = "./WordLists/training_pairs.txt"
train_file = "./WordLists/train.txt"
test_file = "./WordLists/test.txt"
data = read_data(infile)
labels = extract_labels(data)
train, test = train_test_split(labels, data)
write_file(train_file, train)
write_file(test_file, test)
if __name__ == '__main__':
main()