-
Notifications
You must be signed in to change notification settings - Fork 10
/
DataGenerator.py
93 lines (83 loc) · 2.73 KB
/
DataGenerator.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
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
import numpy as np
class kFoldGenerator():
'''
Data Generator
'''
k = -1 # the fold number
x_list = [] # x list with length=k
y_list = [] # x list with length=k
# Initializate
def __init__(self, x, y):
if len(x) != len(y):
assert False, 'Data generator: Length of x or y is not equal to k.'
self.k = len(x)
self.x_list = x
self.y_list = y
# Get i-th fold
def getFold(self, i):
isFirst = True
for p in range(self.k):
if p != i:
if isFirst:
train_data = self.x_list[p]
train_targets = self.y_list[p]
isFirst = False
else:
train_data = np.concatenate((train_data, self.x_list[p]))
train_targets = np.concatenate((train_targets, self.y_list[p]))
else:
val_data = self.x_list[p]
val_targets = self.y_list[p]
return train_data, train_targets, val_data, val_targets
# Get all data x
def getX(self):
All_X = self.x_list[0]
for i in range(1, self.k):
All_X = np.append(All_X, self.x_list[i], axis=0)
return All_X
# Get all label y (one-hot)
def getY(self):
All_Y = self.y_list[0][2:-2]
for i in range(1, self.k):
All_Y = np.append(All_Y, self.y_list[i][2:-2], axis=0)
return All_Y
# Get all label y (int)
def getY_int(self):
All_Y = self.getY()
return np.argmax(All_Y, axis=1)
class DominGenerator():
'''
Domin Generator
'''
k = -1 # the fold number
l_list = [] # length of each domin
d_list = [] # d list with length=k
# Initializate
def __init__(self, len_list):
self.l_list = len_list
self.k = len(len_list)
# Get i-th fold
def getFold(self, i):
isFirst = True
isFirstVal = True
j = 0 #1~9
ii = 0 #1~10
for l in self.l_list:
if ii != i:
a = np.zeros((l, 9), dtype=int)
a[:, j] = 1
if isFirst:
train_domin = a
isFirst = False
else:
train_domin = np.concatenate((train_domin, a))
j += 1
else:
if isFirstVal:
val_domin = np.zeros((l, 9), dtype=int)
isFirstVal = False
else:
a = np.zeros((l, 9), dtype=int)
val_domin = np.concatenate((val_domin, a))
ii += 1
return train_domin, val_domin