-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.py
225 lines (190 loc) · 5.2 KB
/
data.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
#!/usr/bin/python
#coding: utf-8
'''
掌管数据存储和载入的文件
'''
import csv
from util.read_conf import config
from util.ds import Tran
from optparse import OptionParser
#import cPickle as pickle
import pickle
import sys
#配置文件存储位置
cfdir = "conf/data_dir.conf"
conf = config(cfdir)
def get_conf():
return conf
#返回一条数据,这个具体用在统计数据等其他地方
def one_tran(dt="total",tp="tran"):
if dt == "total":
f = open(conf["user_dir"])
elif dt == "dev":
f = open(conf["test128"])
elif dt == "train":
f = open(conf["train70"])
elif dt == "test":
f = open(conf["pred129"])
else:
print "未定义文件名%s"%(dt)
sys.exit(1)
reader = csv.reader(f)
count = 0
if dt == "test":
count += 1
for line in reader:
if count == 0:
count += 1
continue
ud,iid,bt,ug,ic,tm = int(line[0]),int(line[1]),int(line[2]),line[3],line[4],line[5]
tran = Tran(ud,iid,bt,ic,tm,ug)
count += 1
if tp == "tran":
yield tran
elif tp == "list":
yield line
elif tp == "list,tran" or tp == "tran,list":
yield [tran,line]
else:
yield ','.join(line)
#存储user文件
def dump_user():
#user文件,开文件
f = open(conf["user_dir"])
reader = csv.reader(f)
t = open(conf["udump_dir"],"wb")
result = []
count = 0
for line in reader:
if count == 0:
count += 1
continue
ud,iid,bt,ug,ic,tm = int(line[0]),int(line[1]),int(line[2]),line[3],line[4],line[5]
tran = Tran(ud,iid,bt,ic,tm,ug)
result.append(tran)
count += 1
if count % 1000000 == 0:
print("进度%sM"%(count/1000000))
print("正在存储")
pickle.dump(result,t,True)
def load_user():
f = open(conf["udump_dir"],"rb")
trans = pickle.load(f)
for tran in trans:
print(tran)
#存储label
def dump_label(data_set):
print data_set
ot = one_tran(data_set)
t = ""
if data_set == "train":
t = open(conf["train_label"],"w")
elif data_set == "dev":
t = open(conf["dev_label"],"w")
else:
print "没做"
sys.exit(1)
count = 0
for tran in ot:
beh = tran.behavior_type
lb = -1
if beh == 1 or beh==2 or beh==3:
lb = 0
elif beh == 4:
lb = 1
else:
print "err"
sys.exit(1)
t.write("%s\n"%(lb))
count += 1
if count % 10000 == 0:
print count
print "done"
def load_label(data_set):
if data_set == "train":
f = open(conf["train_label"])
elif data_set == "dev":
f = open(conf["dev_label"])
else:
print "没做"
sys.exit(1)
result = f.readlines()
result = [int(i) for i in result]
return result
def load_data(data_set,rd):
if data_set == "train":
f = open(conf["train_dir"])
elif data_set == "dev":
f = open(conf["dev_dir"])
else:
print "没做"
sys.exit(1)
result = []
f.readline()
if data_set == "train":
count = 0
for line in f:
sp = line.split(',')
sp = [int(i) for i in sp]
result.append(sp)
count += 1
if count % rd == 0:
yield result
result = []
else:
count = 0
for line in f:
sp = line.split(',')
sp = [int(i) for i in sp]
result.append(sp)
count += 1
if count % 20000 == 0:
print count
yield result
def load_data_total(data_set,rd):
if data_set == "train":
f = open(conf["train_dir"])
elif data_set == "dev":
f = open(conf["dev_dir"])
else:
print "没做"
sys.exit(1)
result = []
f.readline()
if data_set == "train":
count = 0
for line in f:
sp = line.split(',')
sp = [int(i) for i in sp]
result.append(sp)
count += 1
if count % 100000 == 0:
print count
if count > rd:
break
return result
else:
count = 0
for line in f:
sp = line.split(',')
sp = [int(i) for i in sp]
result.append(sp)
count += 1
if count % 100000 == 0:
print count
return result
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-d","--data",dest="data",action="store",\
help=u"存储数据种类",type="string")
parser.add_option("-a","--action",dest="action",action="store",\
help=u"选择动作是存储还是装载装载用来测试",type="string")
(options, args) = parser.parse_args()
if options.action == "dump":
if options.data == "user":
dump_user()
elif options.action == "load":
if options.data == "user":
load_user()
if options.action == "label":
dump_label(options.data)