-
Notifications
You must be signed in to change notification settings - Fork 1
/
SalesPrediction_mv_Online.py
259 lines (168 loc) · 7.95 KB
/
SalesPrediction_mv_Online.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
import tensorflow as tf
import matplotlib as mplt
mplt.use('agg') # Must be before importing matplotlib.pyplot or pylab!
import matplotlib.pyplot as plt
import numpy as np
import random
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from math import sqrt
import csv
class RNNConfig():
input_size = 1
num_steps = 5
lstm_size = 32
num_layers = 1
keep_prob = 0.8
batch_size = 1
init_learning_rate = 0.01
learning_rate_decay = 0.99
test_ratio = 0.2
fileName = 'store165_2.csv'
graph = tf.Graph()
features = 4
column_min_max = [[0, 10000], [1, 7]]
columns = ['Sales', 'DayOfWeek', 'SchoolHoliday', 'Promo']
store = 285
config = RNNConfig()
def segmentation(data):
seq = [price for tup in data[[config.columns[0], config.columns[1], config.columns[2], config.columns[3]]].values for price in tup]
seq = np.array(seq)
# split into items of features
seq = [np.array(seq[i * config.features: (i + 1) * config.features])
for i in range(len(seq) // config.features)]
# 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)])
# get only sales value
y = [[y[i][0]] for i in range(len(y))]
y = np.asarray(y)
return X, y
def scale(data):
for i in range (len(config.column_min_max)):
data[config.columns[i]] = (data[config.columns[i]] - config.column_min_max[i][0]) / ((config.column_min_max[i][1]) - (config.column_min_max[i][0]))
return data
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 ---------------------------------
original_data = store_data.copy()
# -------------- processing train data---------------------------------------
scaled_train_data = scale(store_data)
train_X, train_y = segmentation(scaled_train_data)
# ----segmenting original test data-----------------------------------------------
nonescaled_X, nonescaled_y = segmentation(original_data)
return train_X, train_y, nonescaled_y
def generate_batches(train_X, train_y,nonescaled_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)
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]
nonesclaed_batch_y = nonescaled_y[j * batch_size: (j + 1) * batch_size]
assert set(map(len, batch_X)) == {config.num_steps}
yield batch_X, batch_y,nonesclaed_batch_y
def mean_absolute_percentage_error(y_true, y_pred):
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 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 plot(RMSE_mean, prediction_vals, nonescaled_y):
fig1 = plt.figure()
fig1 = plt.figure(dpi=100, figsize=(20, 7))
days = range(len(RMSE_mean))
plt.plot(days, RMSE_mean, label='RMSE mean')
plt.legend(loc='upper left', frameon=False)
plt.xlabel("day")
plt.ylabel("RMSE")
# plt.ylim((min(test_y), max(test_y)))
plt.grid(ls='--')
plt.savefig("Sales RMSE mean mv online store 285 .png", format='png', bbox_inches='tight', transparent=False)
plt.close()
fig2 = plt.figure()
fig2 = plt.figure(dpi=100, figsize=(20, 7))
days = range(len(nonescaled_y))
plt.plot(days, nonescaled_y, label='true sales')
plt.plot(days, prediction_vals, label='predicted sales')
plt.legend(loc='upper left', frameon=False)
plt.xlabel("day")
plt.ylabel("sales")
# plt.ylim((min(test_y), max(test_y)))
plt.grid(ls='--')
plt.savefig("sales price Prediction VS Truth mv online store 285.png", format='png', bbox_inches='tight', transparent=False)
plt.close()
def write_results(RMSE_mean,pred_vals,true_vals,name):
with open(name, "w") as f:
writer = csv.writer(f)
writer.writerows(zip(true_vals, pred_vals,RMSE_mean))
def train_test():
train_X, train_y, nonescaled_y= pre_process()
# Create the graph object
graph = tf.Graph()
# Add nodes to the graph
with 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.features], 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=graph) as sess:
tf.set_random_seed(1)
tf.global_variables_initializer().run()
iteration = 1
RMSE = []
RMSE_mean=[]
prediction_vals = []
for batch_X, batch_y, nonescaled_batch_y in generate_batches(train_X, train_y, nonescaled_y, config.batch_size):
train_data_feed = {
inputs: batch_X,
targets: batch_y,
learning_rate: config.init_learning_rate,
keep_prob: config.keep_prob
}
test_pred = sess.run(prediction, train_data_feed)
test_pred[0][0] = (test_pred[0][0] * (config.column_min_max[0][1] - config.column_min_max[0][0])) + config.column_min_max[0][0]
prediction_vals.append(test_pred[0])
meanSquaredError = mean_squared_error(nonescaled_batch_y, test_pred[0])
rootMeanSquaredError = sqrt(meanSquaredError)
print("RMSE:", rootMeanSquaredError)
RMSE_mean.append(np.mean(RMSE))
RMSE.append(rootMeanSquaredError)
train_loss, _, value = sess.run([loss, minimize, val1], train_data_feed)
print("Iteration: {}".format(iteration),
"Train loss: {:.6f}".format(train_loss))
iteration += 1
saver = tf.train.Saver()
saver.save(sess, "checkpoints_stock/stock_pred_online.ckpt")
prediction_vals = np.asarray(prediction_vals).flatten()
prediction_vals = prediction_vals.tolist()
nonescaled_y = np.asarray(nonescaled_y).flatten()
nonescaled_y = nonescaled_y.tolist()
write_results(RMSE_mean,prediction_vals,nonescaled_y,"Sales Online mv1 results.csv")
plot(RMSE_mean,prediction_vals,nonescaled_y)
if __name__ == '__main__':
train_test()