-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
354 lines (283 loc) · 14.2 KB
/
app.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import pandas as pd
import numpy as np
import streamlit as st
import plotly.express as px
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
import keras
from keras.layers import Dense
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dropout
from keras.wrappers.scikit_learn import KerasRegressor
st.beta_set_page_config(
layout="wide",
initial_sidebar_state="expanded",
page_title='PREDIA – Modelo Híbrido Multifatorial',
page_icon=None,
)
# função para carregar o dataset
@st.cache
def get_data():
data = pd.read_csv("model/dataset.csv")
return data
# função para retornar as colunas necessarias para o lstm
def get_features_to_drop_lstm():
return ['DATA', 'VENDAS', 'SEMANA_PAGAMENTO', 'PRECIPITACAO', 'TEMPERATURA', 'POS_DATA_FESTIVA', 'FDS', 'VESPERA_DATA_FESTIVA', 'ALTA_TEMPORADA']
# funcao para retornar o modelo base de lstm
def get_base_model_lstm():
lstm = Sequential()
lstm.add(LSTM(190, return_sequences=True))
lstm.add(Dropout(0.2))
lstm.add(LSTM(190, return_sequences=True))
lstm.add(Dropout(0.2))
lstm.add(LSTM(190, return_sequences=False))
lstm.add(Dropout(0.2))
lstm.add(Dense(1))
lstm.compile(loss='mean_squared_error', optimizer='adam')
return lstm
# função para treinar o modelo lstm
@st.cache(allow_output_mutation=True)
def train_model_lstm():
# separa o dataset em treino e teste
data = get_data()
x = data.drop(columns=get_features_to_drop_lstm(),axis=1)
y = data["VENDAS"]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.05, random_state=1, shuffle=False)
# realiza o feature scaling
scaler = preprocessing.MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# reshape to 3D
train_X = X_train.reshape((X_train.shape[0], 1, X_train.shape[1]))
train_y = np.array(y_train).reshape((y_train.shape[0], 1, 1))
# instancia o modelo
lstm_regressor = KerasRegressor(build_fn=get_base_model_lstm)
# treina o modelo
lstm_regressor.fit(train_X, train_y, epochs=200, batch_size=20, shuffle=False, verbose=False)
# retorna
return lstm_regressor
# função para retornar as colunas necessarias para o gb
def get_features_to_drop_gb():
return ['DATA', 'VENDAS', 'SEMANA_PAGAMENTO', 'PRECIPITACAO', 'FDS', 'UMIDADE', 'TEMPERATURA', 'VESPERA_DATA_FESTIVA', 'FDS', 'UMIDADE', 'TEMPERATURA', 'VESPERA_DATA_FESTIVA', 'POS_DATA_FESTIVA']
# função para treinar o modelo gb
def train_model_gb():
# separa o dataset em treino e teste
data = get_data()
x = data.drop(columns=get_features_to_drop_gb(), axis=1)
y = data["VENDAS"]
X_train, X_test, y_train, y_test = train_test_split(
x, y, test_size=0.05, random_state=1, shuffle=False)
# instancia o modelo
gb_regressor = GradientBoostingRegressor(alpha=0.9, ccp_alpha=0.0, criterion='mse', init=None,
learning_rate=0.1, loss='ls', max_depth=25,
max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=21, min_samples_split=16,
min_weight_fraction_leaf=0.0, n_estimators=139,
n_iter_no_change=None, presort='deprecated',
random_state=1, subsample=1.0, tol=0.0001,
validation_fraction=0.1, verbose=0, warm_start=False)
# realiza o feature scaling
scaler = preprocessing.MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# treina o modelo
gb_regressor.fit(X_train, y_train)
# retorna
return gb_regressor
# função para retornar as colunas necessarias para o mlp
def get_features_to_drop_mlp():
return ['DATA', 'VENDAS', 'SEMANA_PAGAMENTO', 'PRECIPITACAO', 'QTD_CONCORRENTES', 'VESPERA_DATA_FESTIVA', 'FDS', 'QTD_CONCORRENTES', 'VESPERA_DATA_FESTIVA', 'FDS', 'ALTA_TEMPORADA']
# função para treinar o modelo mlp
def train_model_mlp():
# separa o dataset em treino e teste
data = get_data()
x = data.drop(columns=get_features_to_drop_mlp(), axis=1)
y = data["VENDAS"]
X_train, X_test, y_train, y_test = train_test_split(
x, y, test_size=0.05, random_state=1, shuffle=False)
# instancia o modelo
mlp_regressor = MLPRegressor(activation='identity', alpha=0.0001, batch_size=300, beta_1=0.9,
beta_2=0.999, early_stopping=True, epsilon=1e-08,
hidden_layer_sizes=(149), learning_rate='constant',
learning_rate_init=0.001, max_fun=15000, max_iter=100,
momentum=0.9, n_iter_no_change=10, nesterovs_momentum=True,
power_t=0.5, random_state=1, shuffle=False, solver='lbfgs',
tol=0.0001, validation_fraction=0.1, verbose=False,
warm_start=True)
# realiza o feature scaling
scaler = preprocessing.MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# treina o modelo
mlp_regressor.fit(X_train, y_train)
# retorna
return mlp_regressor
########################################################################
# criando um dataframe
data = get_data()
# treinando os modelos
LSTM = train_model_lstm()
GB = train_model_gb()
MLP = train_model_mlp()
########################################################################
# título
st.title("PREDIA – Modelo Multifatorial")
# subtítulo
githublink = """
App utilizado para exibir a solução de Machine Learning construída para a predição de almoços do Restaurante Nostra Bréscia (<a href="https://github.com/LuisValgoi/predia" target="_blank">Github</a>)
"""
st.markdown(githublink, unsafe_allow_html=True)
########################################################################
# verificando o dataset
st.subheader("Selecionando apenas um pequeno conjunto de atributos")
allcols = ['DATA', 'VENDAS', 'FDS', 'DATA_FESTIVA', 'VESPERA_DATA_FESTIVA', 'POS_DATA_FESTIVA', 'FERIADO', 'ALTA_TEMPORADA', 'QTD_CONCORRENTES', 'TEMPERATURA', 'UMIDADE', 'VENDAS_ONTEM']
defaultcols = ['DATA', 'VENDAS', 'FDS', 'DATA_FESTIVA', 'FERIADO', 'ALTA_TEMPORADA', 'QTD_CONCORRENTES', 'TEMPERATURA', 'VENDAS_ONTEM']
cols = st.multiselect("", allcols, default=defaultcols)
st.dataframe(data[cols].head(10))
########################################################################
# plot a distribuição dos dados
st.subheader("Distribuição vendas por período")
faixa_valores = st.slider("Faixa de Vendas de Almoços", int(data.VENDAS.min()), int(data.VENDAS.max()), (100, 150))
dados = data[data['VENDAS'].between(left=faixa_valores[0], right=faixa_valores[1])]
f1 = px.histogram(dados, x="DATA", y="VENDAS", nbins=100,title="Distribuição de Vendas de Almoço")
f1.update_xaxes(title="Período")
f1.update_yaxes(title="Almoços Vendidos")
st.plotly_chart(f1)
########################################################################
# plot a relação de VENDAS x FERIADO
st.subheader("Relação de Almoços Vendidos por Quantidade de Concorrentes Abertos")
f7 = px.box(dados, x="QTD_CONCORRENTES", y="VENDAS",color="QTD_CONCORRENTES", notched=True)
f7.update_xaxes(title="Quantidade de Concorrentes Abertos")
f7.update_yaxes(title="Almoços Vendidos")
st.plotly_chart(f7)
########################################################################
# plot a relação de VENDAS x FERIADO
st.subheader("Relação de Almoços Vendidos por Temperatura")
f8 = px.box(dados, x="TEMPERATURA", y="VENDAS",color="TEMPERATURA", notched=True)
f8.update_xaxes(title="Temperatura")
f8.update_yaxes(title="Almoços Vendidos")
st.plotly_chart(f8)
########################################################################
# plot a relação de VENDAS x FDS
st.subheader("Relação de Almoços Vendidos por Finais de Semanas")
f2 = px.box(dados, x="FDS", y="VENDAS", color="FDS", notched=True)
f2.update_xaxes(title="Finais de Semana")
f2.update_yaxes(title="Almoços Vendidos")
st.plotly_chart(f2)
########################################################################
# plot a relação de VENDAS x FERIADO
st.subheader("Relação de Almoços Vendidos por Feriados")
f3 = px.box(dados, x="FERIADO", y="VENDAS",color="FERIADO", notched=True)
f3.update_xaxes(title="Feriados")
f3.update_yaxes(title="Almoços Vendidos")
st.plotly_chart(f3)
########################################################################
# plot a relação de VENDAS x VESPERA_DATA_FESTIVA
st.subheader("Relação de Almoços Vendidos por Vésperas de Datas Festivas")
f4 = px.box(dados, x="VESPERA_DATA_FESTIVA", y="VENDAS",color="VESPERA_DATA_FESTIVA", notched=True)
f4.update_xaxes(title="Vésperas de Datas Festivas")
f4.update_yaxes(title="Almoços Vendidos")
st.plotly_chart(f4)
########################################################################
# plot a relação de VENDAS x POS_DATA_FESTIVA
st.subheader("Relação de Almoços Vendidos por Dias Após Datas Festivas")
f5 = px.box(dados, x="POS_DATA_FESTIVA", y="VENDAS",color="POS_DATA_FESTIVA", notched=True)
f5.update_xaxes(title="Dias Após Datas Festivas")
f5.update_yaxes(title="Almoços Vendidos")
st.plotly_chart(f5)
########################################################################
# plot a relação de VENDAS x FERIADO
st.subheader("Relação de Almoços Vendidos por Alta Temporada")
f6 = px.box(dados, x="ALTA_TEMPORADA", y="VENDAS",color="ALTA_TEMPORADA", notched=True)
f6.update_xaxes(title="Alta Temporada")
f6.update_yaxes(title="Almoços Vendidos")
st.plotly_chart(f6)
########################################################################
html = """
<style>
.sidebar .sidebar-content {
padding: 2rem 1rem;
}
.reportview-container .main .block-container {
padding: 2rem 1rem 10rem;
}
</style>
"""
st.markdown(html, unsafe_allow_html=True)
st.sidebar.title("Simule uma predição")
st.sidebar.subheader("Fatores Histórico")
VENDAS_ONTEM = st.sidebar.number_input("Quantos almoços foram vendidos ontem?", value=int(data.VENDAS_ONTEM.mean()), step=1)
st.sidebar.subheader("Fatores da Concorrência")
QTD_CONCORRENTES = st.sidebar.number_input("Quantos concorrentes estarão abertos?", value=int(data.QTD_CONCORRENTES.max()), step=1)
st.sidebar.subheader("Fatores de Natureza")
TEMPERATURA = st.sidebar.number_input("Qual a previsão de temperatura (em °C)?", value=int(data.TEMPERATURA.mean()), step=5)
UMIDADE = st.sidebar.number_input("Qual a previsão de umidade?", value=int(data.UMIDADE.mean()), step=5)
st.sidebar.subheader("Fatores do Local")
FERIADO = st.sidebar.selectbox("Será um feriado?", ("Sim", "Não"))
ALTA_TEMPORADA = st.sidebar.selectbox("Será uma data de alta temporada (Março à Dezembro) ?", ("Sim", "Não"))
DATA_FESTIVA = st.sidebar.selectbox("Será uma data festiva?", ("Sim", "Não"))
POS_DATA_FESTIVA = st.sidebar.selectbox("Será após uma data festiva?", ("Sim", "Não"))
# transformando o dado de entrada em valor binário
FERIADO = 1 if FERIADO == "Sim" else 0
ALTA_TEMPORADA = 1 if ALTA_TEMPORADA == "Sim" else 0
DATA_FESTIVA = 1 if DATA_FESTIVA == "Sim" else 0
POS_DATA_FESTIVA = 1 if POS_DATA_FESTIVA == "Sim" else 0
########################################################################
# inserindo um botão na tela
btn_predict = st.sidebar.button("Realizar Predição")
# verifica se o botão foi acionado
if btn_predict:
########################################################################
x = data.drop(columns=get_features_to_drop_lstm(), axis=1)
y = data["VENDAS"]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.05, random_state=1, shuffle=False)
scaler = preprocessing.MinMaxScaler()
scaler.fit_transform(X_train)
train_X = np.array(X_train).reshape((X_train.shape[0], 1, X_train.shape[1]))
train_y = np.array(y_train).reshape((y_train.shape[0], 1, 1))
lstm_features = []
lstm_features.append(DATA_FESTIVA)
lstm_features.append(FERIADO)
lstm_features.append(QTD_CONCORRENTES)
lstm_features.append(UMIDADE)
lstm_features.append(VENDAS_ONTEM)
lstm_features = scaler.transform([lstm_features])
lstm_features = np.array(lstm_features).reshape((lstm_features.shape[0], 1, lstm_features.shape[1]))
lstm_y_pred = LSTM.predict(lstm_features).round().astype(int)
########################################################################
x = data.drop(columns=get_features_to_drop_gb(), axis=1)
y = data["VENDAS"]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.05, random_state=1, shuffle=False)
scaler = preprocessing.MinMaxScaler()
scaler.fit_transform(X_train)
gb_features = []
gb_features.append(ALTA_TEMPORADA)
gb_features.append(DATA_FESTIVA)
gb_features.append(FERIADO)
gb_features.append(QTD_CONCORRENTES)
gb_features.append(VENDAS_ONTEM)
gb_features = scaler.transform([gb_features])
gb_y_pred = GB.predict(gb_features).round().astype(int)[0]
########################################################################
x = data.drop(columns=get_features_to_drop_mlp(), axis=1)
y = data["VENDAS"]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.05, random_state=1, shuffle=False)
scaler = preprocessing.MinMaxScaler()
scaler.fit_transform(X_train)
mlp_features = []
mlp_features.append(DATA_FESTIVA)
mlp_features.append(FERIADO)
mlp_features.append(POS_DATA_FESTIVA)
mlp_features.append(TEMPERATURA)
mlp_features.append(UMIDADE)
mlp_features.append(VENDAS_ONTEM)
mlp_features = scaler.transform([mlp_features])
mlp_y_pred = MLP.predict(mlp_features).round().astype(int)[0]
########################################################################
qtd_almoco_ensemble = (gb_y_pred + mlp_y_pred + lstm_y_pred) / 3.1
# printa o texto
st.sidebar.subheader(f"SERÃO VENDIDOS CERCA DE {int(round(qtd_almoco_ensemble))} ALMOÇOS")