-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathactive-select.py
executable file
·323 lines (303 loc) · 12.5 KB
/
active-select.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/python3 -W all
"""
select-active.py: select training lines for active learning
usage: select-active.py -d dataFile -p probFile [-c|-r|-l|-m|-e|-S] [-R] [-a] [-s simFile] [-x]
note: command line arguments:
-d: data file, lines correspond with those of the probabilities file
-p: file with probabilities; line format: class1 prob1 class2 prob2 ...
-c: select 50% of the data based on confidence; rest at random
-m: select data by margin between best and second best label
-r: select data randomly
-e: select data by entropy of all classes
-l: select longest lengths in characters
-S: select highest similarity
-R: reverse selection: not the worst but the best
-a: output all input data, first selected, then rest
-s: similarity file; line format: one float per line, one per data file item
-x: print score in output before each line
-z: size of output in lines
-h: fill this fraction of the output with random samples (default 0.5)
-t: select by time: oldest first
-D: do not delete duplicate tweets (default: delete)
-w: in random selection leave replace the selected data in the source
20170718 erikt(at)xs4all.nl
"""
import getopt
import math
import random
import sys
COMMAND = sys.argv.pop(0)
USAGE = "usage: "+COMMAND+" -p probFile -d dataFile [-c|-r|-l|-m|-e|-S|-t] [-R] [-a] [-s simFile] [-x] [-z size] -D -w"
EXPERIMENTSEPARATOR = "#"
sampleSize = 5503
dataFile = ""
probFile = ""
reverse = False
useRandom = False
useMargin = False
useConfidence = False
useLength = False
useEntropyAll= False
useSimilarity= False
useTime = False
outputAll = False
printScore = False
randomFraction = 0.5
deleteDuplicates = True
randomWithReplacement = False
simFile = ""
data = []
try: options = getopt.getopt(sys.argv,"acd:Deh:lmp:rRs:Stxwz:",[])
except: sys.exit(USAGE)
nbrOfMethods = 0
for option in options[0]:
if option[0] == "-c": useConfidence = True
elif option[0] == "-d": dataFile = option[1]
elif option[0] == "-e": useEntropyAll = True; nbrOfMethods += 1
elif option[0] == "-p": probFile = option[1]
elif option[0] == "-r": useRandom = True; nbrOfMethods += 1
elif option[0] == "-l": useLength = True; nbrOfMethods += 1
elif option[0] == "-m": useMargin = True; nbrOfMethods += 1
elif option[0] == "-t": useTime = True; nbrOfMethods += 1
elif option[0] == "-R": reverse = True
elif option[0] == "-a": outputAll = True
elif option[0] == "-s": simFile = option[1]
elif option[0] == "-S": useSimilarity = True
elif option[0] == "-x": printScore = True
elif option[0] == "-z": sampleSize = int(option[1])
elif option[0] == "-h": randomFraction = float(option[1])
elif option[0] == "-D": deleteDuplicates = False
elif option[0] == "-w": randomWithReplacement = True
else: sys.exit(USAGE)
if dataFile == "": sys.exit(USAGE)
if probFile == "" and \
(useEntropyAll or useMargin or useConfidence): sys.exit(USAGE)
if nbrOfMethods > 1: sys.exit(COMMAND+": multiple selection methods chosen!")
if randomFraction >= 0 and randomFraction <= 1:
halfTarget = (1.0-randomFraction)*float(sampleSize)
else:
sysexit(COMMAND+": unexpected value for random fraction: "+str(randomFraction))
def selectTime(data,sampleSize):
selected = []
while len(selected) < sampleSize and len(data) > 0:
if reverse: index = -1
else: index = 0
data[index]["score"] = 1.0
selected.append(data[index])
data.pop(index)
if len(selected) < sampleSize:
sys.exit(COMMAND+": selectTime(): too few data!\n")
return({"selected":selected,"rest":data})
def selectRandom(data,sampleSize):
selected = []
while len(selected) < sampleSize and len(data) > 0:
index = int(len(data)*random.random())
data[index]["score"] = 1.0
selected.append(data[index])
if not randomWithReplacement: data.pop(index)
if len(selected) < sampleSize:
sys.exit(COMMAND+": selectRandom(): too few data!\n")
return({"selected":selected,"rest":data})
def getProbs(line):
probs = {}
nbrOfExps = 1
# fields format: exp1-label1 exp1-conf1 ... exp1-label12 exp1-conf12 exp2-label1
fields = line["scores"].split()
if len(fields) <= 0: sys.exit(COMMAND+": getProbs: empty list: fields")
if len(fields) < 3: sys.exit(COMMAND+\
": getProbs: unexpected number of probabilities on line: "+str(len(fields)))
i = 0
while i < len(fields):
if fields[i] == EXPERIMENTSEPARATOR: nbrOfExps += 1; i += 1
else:
if len(fields) < i+2: sys.exit(COMMAND+": incomplete line ("+str(i)+"): "+str(fields))
thisClass, value = fields[i], fields[i+1]
if thisClass in probs: probs[thisClass] += float(value) # 20170924 was 1.0
else: probs[thisClass] = float(value) # was 1.0
i += 2
for c in probs: probs[c] /= nbrOfExps
return(probs)
def computeConfidence(line):
similarity = 1.0
if "similarity" in line:
try: similarity = line["similarity"]
except: sys.exit(COMMAND+": computeConfidence: "+line["similarity"]+" is not a number\n")
probs = getProbs(line)
maxProb = max(probs.values())
if not reverse: return(maxProb*similarity)
else: return(maxProb/similarity)
def selectConfidence(data,sampleSize):
selected = []
rest = []
counter = 0
for line in data:
counter += 1
line["score"] = computeConfidence(line)
if len(selected) >= halfTarget and \
((not reverse and line["score"] >= selected[-1]["score"]) or \
(reverse and line["score"] <= selected[-1]["score"])):
rest.append(line)
else:
selected.append(line)
selected.sort(key=lambda item: item["score"],reverse=reverse)
while len(selected) > halfTarget:
element = selected.pop(-1)
rest.append(element)
randomHalf = selectRandom(rest,sampleSize-halfTarget)
selected.extend(randomHalf["selected"])
return({"selected":selected,"rest":randomHalf["rest"]})
def computeEntropyAll(line):
entropy = 0.0
similarity = 1.0
if "similarity" in line:
try: similarity = line["similarity"]
except: sys.exit(COMMAND+": computeConfidence: "+line["similarity"]+" is not a number\n")
probs = getProbs(line)
for thisClass in probs:
entropy += -probs[thisClass]*math.log(probs[thisClass])/math.log(2)
if not reverse: return(entropy*similarity)
else: return(entropy/similarity)
def selectEntropyAll(data,sampleSize):
selected = []
rest = []
for line in data:
line["score"] = computeEntropyAll(line)
if len(selected) >= halfTarget and \
((not reverse and line["score"] <= selected[-1]["score"]) or
(reverse and line["score"] >= selected[-1]["score"])):
rest.append(line)
else:
selected.append(line)
selected.sort(key=lambda item: item["score"],reverse=not reverse)
while len(selected) > halfTarget:
element = selected.pop(-1)
rest.append(element)
randomHalf = selectRandom(rest,sampleSize-halfTarget)
selected.extend(randomHalf["selected"])
return({"selected":selected,"rest":randomHalf["rest"]})
def computeMargin(line):
similarity = 1.0
if "similarity" in line:
try: similarity = line["similarity"]
except: sys.exit(COMMAND+": computeConfidence: "+line["similarity"]+" is not a number\n")
probs = getProbs(line)
values = sorted(probs.values(),reverse=True)
if len(values) < 2: margin = 0.0
else: margin = values[1]/values[0]
if not reverse: return(margin*similarity)
else: return(margin/similarity)
def selectMargin(data,sampleSize):
selected = []
rest = []
for line in data:
line["score"] = computeMargin(line)
if len(selected) >= halfTarget and \
((not reverse and line["score"] <= selected[-1]["score"]) or
(reverse and line["score"] >= selected[-1]["score"])):
rest.append(line)
else:
selected.append(line)
selected.sort(key=lambda item: item["score"],reverse=not reverse)
while len(selected) > halfTarget:
element = selected.pop(-1)
rest.append(element)
randomHalf = selectRandom(rest,sampleSize-halfTarget)
selected.extend(randomHalf["selected"])
return({"selected":selected,"rest":randomHalf["rest"]})
def selectLength(data,sampleSize):
selected = []
rest = []
for line in data:
line["score"] = float(len(line["data"]))
similarity = 1.0
if "similarity" in line:
try: similarity = line["similarity"]
except: sys.exit(COMMAND+": setLength: "+line["similarity"]+" is not a number\n")
if not reverse: line["score"] *= similarity
else: line["score"] /= similarity
if len(selected) >= halfTarget and \
((not reverse and line["score"] <= selected[-1]["score"]) or \
(reverse and line["score"] >= selected[-1]["score"])):
rest.append(line)
else:
selected.append(line)
selected.sort(key=lambda item: item["score"],reverse=not reverse)
while len(selected) > halfTarget:
element = selected.pop(-1)
rest.append(element)
randomHalf = selectRandom(rest,sampleSize-halfTarget)
selected.extend(randomHalf["selected"])
return({"selected":selected,"rest":randomHalf["rest"]})
def selectSimilarity(data,sampleSize):
selected = []
rest = []
for line in data:
if not "similarity" in line: line["score"] = 1.0
else: line["score"] = line["similarity"]
if len(selected) >= halfTarget and \
((not reverse and line["score"] <= selected[-1]["score"]) or \
(reverse and line["score"] >= selected[-1]["score"])):
rest.append(line)
else:
selected.append(line)
selected.sort(key=lambda item: item["score"],reverse=not reverse)
while len(selected) > halfTarget:
element = selected.pop(-1)
rest.append(element)
randomHalf = selectRandom(rest,sampleSize-halfTarget)
selected.extend(randomHalf["selected"])
return({"selected":selected,"rest":randomHalf["rest"]})
### main
try: dataStream = open(dataFile,"r")
except: sys.exit(COMMAND+": cannot read file "+dataFile+"\n")
if probFile != "":
try: probStream = open(probFile,"r")
except: sys.exit(COMMAND+": cannot read file "+probFile+"\n")
if simFile != "":
try: simStream = open(simFile,"r")
except: sys.exit(COMMAND+": cannot read file "+simFile+"\n")
seen = {}
probsSeen = False
for dataLine in dataStream:
dataLine = dataLine.rstrip()
line = { "data":dataLine }
if probFile != "":
probLine = probStream.readline()
if probLine != "":
probLine = probLine.rstrip()
line["scores"] = probLine
probsSeen = True
if simFile != "":
simLine = simStream.readline()
if simLine == "": sys.exit(COMMAND+": too few lines in similarity file "+simFile)
simLine = simLine.rstrip()
try: line["similarity"] = float(simLine)
except: sys.exit(COMMAND+": "+simLine+" is not a number")
if not dataLine in seen or not deleteDuplicates:
data.append(line)
seen[dataLine] = True
if probFile != "": probStream.close()
dataLine = dataStream.readline()
dataStream.close()
if dataLine != "": sys.exit(COMMAND+": too many lines in data file "+dataFile)
if simFile != "":
simLine = simStream.readline()
simStream.close()
if simLine != "": sys.exit(COMMAND+": too many lines in sim file "+simFile)
if useRandom: selectResults = selectRandom(data,sampleSize)
elif useTime: selectResults = selectTime(data,sampleSize)
elif useLength: selectResults = selectLength(data,sampleSize)
elif useMargin and probsSeen: selectResults = selectMargin(data,sampleSize)
elif useConfidence and probsSeen: selectResults = selectConfidence(data,sampleSize)
elif useEntropyAll and probsSeen: selectResults = selectEntropyAll(data,sampleSize)
elif useSimilarity: selectResults = selectSimilarity(data,sampleSize)
else: selectResults = selectRandom(data,sampleSize)
for line in selectResults["selected"]:
if printScore: print("%0.3f" % (line["score"]),end=" ")
print("%s" % (line["data"]))
if outputAll:
for line in selectResults["rest"]:
if printScore:
if not "score" in line: line["score"] = 0.0
print("%0.3f" % (line["score"]),end=" ")
print("%s" % (line["data"]))