-
Notifications
You must be signed in to change notification settings - Fork 2
/
createData.py
182 lines (143 loc) · 4.32 KB
/
createData.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
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
180
from GainRatio import *
import re
def createData(arg, withLabels):
dataFile = open(arg)
parameters = []
data = []
l = 0
for line in dataFile:
line = re.sub('\t', ' ', line)
for i in range(100):
line = re.sub(' ', ' ', line)
line = re.sub(' ', '', line)
for i in range(20):
line = re.sub(',,', ',?,', line)
line = re.sub(' ', ',', line)
line = re.sub('\n', '', line)
line = line.split(',')
if l == 0:
parameters = line
#del parameters[0]
del parameters[-1]
l = 1
continue
data.append(line)
X = []
y = []
for i in range(0, len(data)):
if withLabels:
X.append(data[i][1 : len(data[i])])
y.append(data[i][0])
# X.append(data[i][0 : len(data[i]) - 1])
# y.append(data[i][len(data[i]) - 1])
else:
X.append(data[i][:])
return (parameters, X, y)
def createLabels(y):
labels = []
for i in range(len(y)):
if y[i] not in labels:
labels.append(y[i])
labels.sort()
return labels
def isFloat(string):
string2 = string.replace('.', '')
string2 = string2.replace('-', '')
if string2.isdigit():
return True
else:
return False
def createCounts(types, i, Xt, y, labels, isContinuous):
c = []
m = 0
if len(types[i]) == 1 and isContinuous:
m = 2
else:
m = len(types[i])
for j in range(m):
c.append([])
for k in range(len(labels)):
c[j].append(0)
for j in range(len(Xt[i])):
idx1 = 0
if Xt[i][j] != '?':
if len(types[i]) == 1 and isContinuous:
if float(Xt[i][j]) <= types[i][0]:
idx1 = 0
else:
idx1 = 1
else:
idx1 = types[i].index(Xt[i][j])
idx2 = labels.index(y[j])
c[idx1][idx2] += 1
return c
def createTypes(X, y, labels, algo):
types = []
counts = []
if len(y) > 1:
Xt = zip(*X)
else:
Xt = zip(*[X])
Xt = list(Xt)
for i in range(len(Xt)):
# TYPES
types_i = []
isContinuous = False
atLeastOneFloat = False
isContNum = 0
for j in range(len(Xt[i])):
if Xt[i][j] not in types_i and Xt[i][j] != '?':
types_i.append(Xt[i][j])
if Xt[i][j] == '?' or isFloat(Xt[i][j]):
isContNum += 1
if Xt[i][j] != '?':
atLeastOneFloat = True
if isContNum == len(Xt[i]) and atLeastOneFloat:
isContinuous = True
gain = []
A = list(Xt[i])
A, y2 = zip(*sorted(zip(A, y)))
#A = [int(x) for x in A]
Aset = set(A)
print(Aset)
for sep in Aset:
print("sep: ", sep)
if sep == '?':
continue
sep = float(sep)
t = [sep]
c = []
for k in range(2):
c.append([])
for j in range(len(labels)):
c[k].append(0)
for k in range(len(A)):
if A[k] != '?':
idx1 = 0
if float(A[k]) <= sep:
idx1 = 0
else:
idx1 = 1
idx2 = labels.index(y2[k])
c[idx1][idx2] += 1
g = 0
if algo.gain == Gain.Type['GainRatio']:
g = GainRatio(c, len(X), algo)
elif algo.gain == Gain.Type['InfoGain']:
g = InfoGain(c, len(X), algo)
gain.append(g)
mx = max(gain)
idx = gain.index(mx)
print(A)
print(idx)
print(gain)
if len(A) > 1 and idx != len(A) - 1 and A[idx + 1] != '?':
t = (float(A[idx]) + float(A[idx + 1])) / 2
else:
t = float(A[idx])
types_i = [t]
types.append(types_i)
# COUNTS
c = createCounts(types, i, Xt, y, labels, isContinuous)
counts.append(c)
return (types, counts)