-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader_v2.py
162 lines (130 loc) · 6.81 KB
/
data_loader_v2.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
import numpy as np
import csv
import time
import pickle
from keras.preprocessing import sequence
def data_loader(X,Y,batch_size):
idx = list(range(len(data)))
random.shuffle(idx)
for i in range(0,len(data),batch_size):
j = np.array(idx[i:min(i+batch_size,len(data))])
yield np.take(X,j),np.take(Y,j)
week = {"Mon":1,"Tue":2,"Wed":3,"Thu":4,"Fri":5,"Sat":6,"Sun":7}
month = {"Dec":12,'Nov':11,'Jan':1}
def data_pre():
time_step_input = 94
time_step_twitter = 7
zero_sum = 50000
with open('../data/preprocessing.csv','r') as f:
reader = csv.reader(f)
twitter = [raw for raw in reader]
# change datetime mat
for i in range(len(twitter)):
tmp = str(twitter[i][3][-4:]) + '-' + str(month[twitter[i][3][4:7]]) + '-' +str(twitter[i][3][8:10]).replace(' ','') + ' ' + str(twitter[i][3][11:19])
tmp = time.strptime(tmp, "%Y-%m-%d %H:%M:%S")
twitter[i][3] = time.mktime(tmp)
d = {}
for raw in twitter:
usr_id = raw[2]
if usr_id in d.keys():
d[usr_id].append(raw)
else:
d[usr_id] = [raw]
# input the length of twitter sentence
# self.inputs_sequence = tf.placeholder(dtype=tf.float32,shape=(self._batch_size,self._time_step_twitter+1),name = "inputs_sequence")
inputs_sequence = []
# input the number of a time sequence
# self.time_sequence = tf.placeholder(dtype=tf.float32,shape=(self._batch_size),name = "time_sequence")
time_sequence = []
# input the reply of each twitter
# self.targets = tf.placeholder(dtype=tf.float32,shape=(self._batch_size,self._time_step_twitter,1),name="targets")
targets = []
# input the delt time of each two twitters.
# self.delt_time = tf.placeholder(dtype=tf.float32,shape=(self._batch_size,self._time_step_twitter,1),name="delt_time")
delt_time = []
# input the predict reply
# self.y = tf.placeholder(dtype = tf.float32,shape = (self._batch_size,1))
y = []
# self.inputs = tf.placeholder(dtype=tf.float32,shape=(self._batch_size,self._time_step_twitter+1,self._time_step_input,embedding_length),name = "inputs")
# inputs = []
inputs = np.zeros([125964,time_step_twitter,time_step_input,100])
cnt = 0
f = open('../data/model', 'rb')
model = pickle.load(f)
f = open('../data/word_frequence', 'rb')
word_freqs = pickle.load(f)
total_zero_sum = 0
for key in d.keys():
if len(d[key]) == 1:
continue
normalization = np.array([i[-1] for i in d[key]],dtype=np.float32)
#均值归一化
# zero = normalization == 0
# if zero.sum() == normalization.shape[0]:
# total_zero_sum += 1
# if zero_sum >0 and zero.sum() == normalization.shape[0]:
# zero_sum -= 1
# continue
# if normalization.std() != 0:
# normalization = (normalization - normalization.mean())/normalization.std()
# else:
# if normalization[0] > 0:
# print("std = 0,then ",normalization[0])
# normalization = np.ones([normalization.shape[0]])
# # if sum(normalization) > normalization.shape[0]:
# # continue
# outlier = normalization > 3
# if outlier.sum() > 0:
# continue
#最大最小值归一化
if normalization.max() == 0 and zero_sum > 0:
zero_sum -= 1
continue
if normalization.max() != 0:
normalization = (normalization - normalization.min()) / (normalization.max() - normalization.min())
for i in range(len(d[key])):
d[key][i][-1] = normalization[i]
for i in range(0,len(d[key]),time_step_twitter):
sample = d[key][i:min(i+time_step_twitter,len(d[key]))]
pad_number = time_step_twitter-len(sample)
# no need padding
# if float(sample[-1][-1]) == 0: # y值为0直接跳过
# continue
y.append([float(sample[-1][-1])])
time_sequence.append(len(sample))
# need padding
targets.append([float(item[-1]) for item in sample[:-1]] + [0]*pad_number)
tmp = np.zeros([time_step_twitter,time_step_input,100])
for i,item in enumerate(sample):
for j,word in enumerate(item[1].split(' ')):
if word_freqs[word] < 10:
continue
# tmp[i][j][:] = model[word]
inputs[cnt][i][j][:] = model[word]
cnt += 1
inputs_sequence.append([len(item[1].split(' ')) for item in sample] + [0]*pad_number)
delt_time.append([[(int(sample[i][3]) - int(sample[i-1][3]))/3600000] for i in range(1,len(sample))] + [[0]]*pad_number)
y = np.array(y)
time_sequence = np.array(time_sequence)
targets = np.array(targets)
inputs_sequence = np.array(inputs_sequence)
delt_time = np.array(delt_time)
print(y[:int(int(y.shape[0]))].shape)
print(time_sequence[:int(int(y.shape[0]))].shape)
print(targets[:int(int(y.shape[0]))].shape)
print(inputs[:int(int(y.shape[0]))].shape)
print(inputs_sequence[:int(int(y.shape[0]))].shape)
print(delt_time[:int(int(y.shape[0]))].shape)
print("total_zero" + str(total_zero_sum))
np.save('../data/lstm_train_y_maxmin_nrl_except_outlier_except0_50000.npy',y[:int(int(y.shape[0]))])
np.save('../data/lstm_train_time_sequence_maxmin_nrl_except_outlier_except0_50000.npy',time_sequence[:int(int(y.shape[0]))])
np.save('../data/lstm_train_targets_maxmin_nrl_except_outlier_except0_50000.npy',targets[:int(int(y.shape[0]))])
np.save('../data/lstm_train_inputs_maxmin_nrl_except_outlier_except0_50000.npy',inputs[:int(int(y.shape[0]))])
np.save('../data/lstm_train_inputs_sequence_maxmin_nrl_except_outlier_except0_50000.npy',inputs_sequence[:int(int(y.shape[0]))])
np.save('../data/lstm_train_delt_time_maxmin_nrl_except_outlier_except0_50000.npy',delt_time[:int(int(y.shape[0]))])
print("data saved!")
return y,time_sequence,targets,inputs,inputs_sequence,delt_time
if __name__ == "__main__":
# y,time_sequence,targets,inputs,inputs_sequence,delt_time = data_loader.data_pre()
y,time_sequence,targets,inputs,inputs_sequence,delt_time = data_pre()
pass