-
Notifications
You must be signed in to change notification settings - Fork 1
/
SalesPrediction_uv.py
318 lines (218 loc) · 9.52 KB
/
SalesPrediction_uv.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
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
import pylab
import matplotlib as mplt
mplt.use('agg') # Must be before importing matplotlib.pyplot or pylab!
import matplotlib.pylab as plt
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from math import sqrt
import csv
np.random.seed(1)
class RNNConfig():
input_size = 1
num_steps = 5
lstm_size = 32
num_layers = 1
keep_prob = 0.8
batch_size = 16
init_learning_rate = 0.01
learning_rate_decay = 0.99
init_epoch = 10 # 5
max_epoch = 95 # 100 or 50
test_ratio = 0.2
fileName = 'store165_2.csv'
graph = tf.Graph()
min = 0
max = 10000
column = 'Sales'
store= 285
config = RNNConfig()
def segmentation(data):
seq = [price for tup in data[[config.column]].values for price in tup]
seq = np.array(seq)
# split into number of input_size
seq = [np.array(seq[i * config.input_size: (i + 1) * config.input_size])
for i in range(len(seq) // config.input_size)]
# split into groups of num_steps
X = np.array([seq[i: i + config.num_steps] for i in range(len(seq) - config.num_steps)])
y = np.array([seq[i + config.num_steps] for i in range(len(seq) - config.num_steps)])
return X, y
def scale(data):
data[config.column] = (data[config.column] - config.min) / (config.max - config.min)
return data
def convert_log(data):
data[config.column] = np.log(data[config.column])
return data
def rescle(test_pred):
prediction = [(pred * (config.max - config.min)) + config.min for pred in test_pred]
return prediction
def convert_from_log(pred_vals):
converted_prediction = [ np.exp(pred) for pred in pred_vals]
return converted_prediction
def RMSPE(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.sqrt(np.mean(np.square(((y_true - y_pred) / y_pred)), axis=0))
def pre_process():
store_data = pd.read_csv(config.fileName)
store_data = store_data.drop(store_data[(store_data.Open == 0) & (store_data.Sales == 0)].index)
#
# store_data = store_data.drop(store_data[(store_data.Open != 0) & (store_data.Sales == 0)].index)
# ---for segmenting original data ---------------------------------
# train_size = int(len(store_data) * (1.0 - config.test_ratio))
test_len = len(store_data[(store_data.Month == 7) & (store_data.Year == 2015)].index)
train_size = int(len(store_data) - test_len)
#744
train_data = store_data[:train_size]
#187
test_data = store_data[train_size:]
original_data = store_data[train_size:]
# -------------- processing train data---------------------------------------
# log_train_data = convert_log(train_data)
scaled_train_data = scale(train_data)
train_X, train_y = segmentation(scaled_train_data)
# -------------- processing test data---------------------------------------
# log_train_data = convert_log(test_data)
scaled_test_data = scale(test_data)
test_X, test_y = segmentation(scaled_test_data)
# ----segmenting original test data-----------------------------------------------
# log_original_data = convert_log(original_data)
nonescaled_X, nonescaled_y = segmentation(original_data)
plot_main_distribution(store_data)
return train_X, train_y, test_X, test_y, nonescaled_y
def generate_batches(train_X, train_y, batch_size):
num_batches = int(len(train_X)) // batch_size
if batch_size * num_batches < len(train_X):
num_batches += 1
batch_indices = range(num_batches)
# random.shuffle(batch_indices)
for j in batch_indices:
batch_X = train_X[j * batch_size: (j + 1) * batch_size]
batch_y = train_y[j * batch_size: (j + 1) * batch_size]
assert set(map(len, batch_X)) == {config.num_steps}
yield batch_X, batch_y
def mean_absolute_percentage_error(y_true, y_pred):
# y_true = y_true.tolist()
# y_pred = y_pred.tolist()
#
# for i in range(len(y_true)):
# if y_true[i] == 0 :
# index = y_true.index(0)
# del y_true[index]
# del y_pred[index]
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
def write_results(true_vals,pred_vals):
true_vals=true_vals.tolist()
pred_vals=pred_vals.tolist()
with open("RNN_uv_results.csv", "w") as f:
writer = csv.writer(f)
writer.writerows(zip(true_vals, pred_vals))
def plot(true_vals, pred_vals, name):
write_results(true_vals,pred_vals)
fig = plt.figure()
fig = plt.figure(dpi=100, figsize=(10, 6))
days = range(len(true_vals))
plt.plot(days, true_vals, label='truth sales')
plt.plot(days, pred_vals, label='pred sales')
plt.legend(loc='upper left', frameon=False)
plt.yscale('log')
plt.xlabel("day")
plt.ylabel("sales")
plt.grid(ls='--')
plt.savefig(name, format='png', bbox_inches='tight', transparent=False)
plt.show()
plt.close()
def plot_main_distribution(store_data):
fig = plt.figure(dpi=100, figsize=(20, 8))
plt.plot(store_data.Date, store_data.Sales, label='sales values')
plt.yscale('log')
plt.legend(loc='upper left', frameon=False)
plt.xlabel("day")
plt.ylabel("sales")
plt.savefig("Original Sales distribution of store 285.png", format='png', bbox_inches='tight', transparent=False)
plt.close()
def train_test():
train_X, train_y, test_X, test_y, nonescaled_y = pre_process()
# Add nodes to the graph
with config.graph.as_default():
tf.set_random_seed(1)
learning_rate = tf.placeholder(tf.float32, None, name="learning_rate")
inputs = tf.placeholder(tf.float32, [None, config.num_steps, config.input_size], name="inputs")
targets = tf.placeholder(tf.float32, [None, config.input_size], name="targets")
keep_prob = tf.placeholder(tf.float32, None, name="keep_prob")
cell = tf.contrib.rnn.LSTMCell(config.lstm_size, state_is_tuple=True, activation=tf.nn.tanh)
val1, _ = tf.nn.dynamic_rnn(cell, inputs, dtype=tf.float32)
val = tf.transpose(val1, [1, 0, 2])
last = tf.gather(val, int(val.get_shape()[0]) - 1, name="last_lstm_output")
# drop_out = tf.nn.dropout(last, keep_prob)
# prediction = tf.layers.dense(drop_out, units=1, activation=None)
# # hidden layer
# hidden = tf.layers.dense(last, units=20, activation=tf.nn.relu)
#
# prediction = tf.contrib.layers.fully_connected(hidden, num_outputs=1, activation_fn=None)
#
weight = tf.Variable(tf.truncated_normal([config.lstm_size, config.input_size]))
bias = tf.Variable(tf.constant(0.1, shape=[config.input_size]))
prediction = tf.matmul(last, weight) + bias
loss = tf.losses.mean_squared_error(targets,prediction)
optimizer = tf.train.AdamOptimizer(learning_rate)
minimize = optimizer.minimize(loss)
# --------------------training------------------------------------------------------
with tf.Session(graph=config.graph) as sess:
tf.set_random_seed(1)
tf.global_variables_initializer().run()
iteration = 1
learning_rates_to_use = [
config.init_learning_rate * (
config.learning_rate_decay ** max(float(i + 1 - config.init_epoch), 0.0)
) for i in range(config.max_epoch)]
for epoch_step in range(config.max_epoch):
current_lr = learning_rates_to_use[epoch_step]
for batch_X, batch_y in generate_batches(train_X, train_y, config.batch_size):
train_data_feed = {
inputs: batch_X,
targets: batch_y,
learning_rate: current_lr,
keep_prob: config.keep_prob
}
train_loss, _, value,ya,la = sess.run([loss, minimize, val1,val,last], train_data_feed)
if iteration % 5 == 0:
print("Epoch: {}/{}".format(epoch_step, config.max_epoch),
"Iteration: {}".format(iteration),
"Train loss: {:.6f}".format(train_loss))
iteration += 1
saver = tf.train.Saver()
saver.save(sess, "checkpoints_sales/sales_pred.ckpt")
# --------------------testing------------------------------------------------------
with tf.Session(graph=config.graph) as sess:
tf.set_random_seed(1)
saver.restore(sess, tf.train.latest_checkpoint('checkpoints_sales'))
test_data_feed = {
learning_rate: 0.0,
keep_prob: 1.0,
inputs: test_X,
targets: test_y,
}
test_pred = sess.run(prediction, test_data_feed)
pred_vals = rescle(test_pred)
# pred_vals = convert_from_log(pred_vals)
pred_vals = np.array(pred_vals)
pred_vals = pred_vals.flatten()
nonescaled_y = nonescaled_y.flatten()
meanSquaredError = mean_squared_error(nonescaled_y, pred_vals)
rootMeanSquaredError = sqrt(meanSquaredError)
print("RMSE:", rootMeanSquaredError)
mae = mean_absolute_error(nonescaled_y, pred_vals)
print("MAE:", mae)
mape = mean_absolute_percentage_error(nonescaled_y, pred_vals)
print("MAPE:", mape)
rmse_val = RMSPE(nonescaled_y, pred_vals)
print("RMSPE:", rmse_val)
plot(nonescaled_y, pred_vals, "RNN Sales Prediction VS Truth uv 285.png")
if __name__ == '__main__':
# pre_process()
train_test()