-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathsampling.py
More file actions
65 lines (58 loc) · 2.04 KB
/
Copy pathsampling.py
File metadata and controls
65 lines (58 loc) · 2.04 KB
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
#coding=utf-8
import csv
import os
import time
import random
from utility import progressBar, timekeeper, doneCount, cutoffLine
from utility import writeCSV, readCSV
from split import FILES
import sys
PRE_DIR = 'splited_data'
TOTAL_10 = 56992193
POSITIVE_10 = 44114
NEGATIVE_10 = 56948079
TOTAL_7 = 46748293
POSITIVE_7 = 46802
NEGATIVE_7 = 46701491
def sampling(window, proportion):
cutoffLine('*')
start_time = time.time()
print 'sampling with propotion %d...' % proportion
exec('negative_needed = POSITIVE_%d * propotion' % window)
sample_times = 20
exec('mod = NEGATIVE_%d / sample_times' % window)
exec('negative_eachtime = negative_needed / sample_times')
training_set = readCSV(PRE_DIR + '/positive_set.csv', int)
## sampling negative example
rfile = file(PRE_DIR + '/' + 'negative_set.csv', 'r')
reader = csv.reader(rfile)
negative_tmp = []
for line in reader:
exec('progressBar(reader.line_num, NEGATIVE_%d)' % window)
negative_tmp.append(map(int, line))
if reader.line_num % mod == 0:
random.shuffle(negative_tmp)
training_set.extend(negative_tmp[0:negative_eachtime])
negative_tmp = []
rfile.close()
wfile = file('data/training_set_%d_%d.csv' % (window, propotion), 'w')
writer = csv.writer(wfile)
random.shuffle(training_set)
writer.writerows(training_set)
wfile.close()
cutoffLine('-')
exec('real_proportion = (len(training_set)- POSITIVE_%d) / float(POSITIVE_%d)'%(window, window))
print "Real proportion: %f" % real_proportion
cutoffLine('*')
end_time = time.time()
duration = timekeeper(start_time, end_time)
print 'It takes %s to sampling with proportion %d'%(duration, proportion)
if __name__ == '__main__':
if len(sys.argv) < 3: print 'Need sample window and propotion'
else:
window = int(sys.argv[1])
propotion = int(sys.argv[2])
print 'Window %d dataset' % window
global PRE_DIR
PRE_DIR = 'splited_data_%d' % window
sampling(window, propotion)