-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
216 lines (159 loc) · 6.04 KB
/
app.py
File metadata and controls
216 lines (159 loc) · 6.04 KB
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
from DModel import DModel
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import altair as alt
import pandas as pd
import streamlit as st
st.set_page_config(
page_title="Predicting monthly scanned receipts for 2022 from 2021", page_icon="🧾", layout="wide"
)
@st.cache_data()
def get_data():
return pd.read_csv("receipts_daily.csv")
@st.cache_data
def convert_df(df):
return df.to_csv(index=False).encode('utf-8')
@st.cache_resource()
def get_chart(data):
hover = alt.selection_single(
fields=["date"],
nearest=True,
on="mouseover",
empty="none",
)
lines = (
alt.Chart(data, height=500, title="Daily # of scanned receipts for 2021 and 2022 prediction")
.mark_line()
.encode(
x=alt.X("# Date", title="Date",axis=alt.Axis(labelAngle=-75)),
y=alt.Y("Receipt_Count", title="# of receipts", scale=alt.Scale(domain=[data["Receipt_Count"].min(), data["Receipt_Count"].max()])),
color="type"
)
)
# Draw points on the line, and highlight based on selection
points = lines.transform_filter(hover).mark_circle(size=65)
# Draw a rule at the location of the selection
tooltips = (
alt.Chart(data)
.mark_rule()
.encode(
x="# Date",#yearmonthdate()
y="Receipt_Count",
opacity=alt.condition(hover, alt.value(0.3), alt.value(0)),
tooltip=[
alt.Tooltip("# Date", title="Date"),
alt.Tooltip("Receipt_Count", title="# of receipts"),
],
)
.add_selection(hover)
)
return (lines + points + tooltips)
st.title("Predicting monthly scanned receipts for 2022 from 2021")
st.write("created by Paul")
n_epochs = st.slider("$$Epochs$$",value=300,min_value=100,max_value=1000)
train_percent = st.slider("$$Training\;Split$$",value=0.90,max_value=0.99,min_value=0.1)
source = get_data()
#https://machinelearningmastery.com/lstm-for-time-series-prediction-in-pytorch/ used as reference
timeseries = source[["Receipt_Count"]].values.astype('float32')
# train-test split for time series
train_size = int(len(timeseries) * train_percent)
test_size = len(timeseries) - train_size
train, test = timeseries[:train_size], timeseries[train_size:]
def create_dataset(dataset, lookback):
"""Transform a time series into a prediction dataset
Args:
dataset: A numpy array of time series, first dimension is the time steps
lookback: Size of window for prediction
"""
X, y = [], []
for i in range(len(dataset)-lookback):
feature = dataset[i:i+lookback]
target = dataset[i+1:i+lookback+1]
X.append(feature)
y.append(target)
return torch.from_numpy(np.array(X)), torch.from_numpy(np.array(y))
lookback = 1
X_train, y_train = create_dataset(train, lookback)
X_test, y_test = create_dataset(test, lookback)
class Config:
def __init__(self, pred_len, seq_len,kernel_size):
self.pred_len = pred_len
self.seq_len = seq_len
self.kernel_size = kernel_size
config = Config(lookback,lookback,1)
model = DModel(config)
optimizer = optim.Adam(model.parameters())
loss_fn = nn.MSELoss()
loader = data.DataLoader(data.TensorDataset(X_train, y_train), shuffle=True, batch_size=8)
scheduler = optim.lr_scheduler.OneCycleLR(optimizer, 2e-3, epochs=n_epochs, steps_per_epoch=len(loader))
for epoch in range(n_epochs):
model.train()
for X_batch, y_batch in loader:
y_pred = model(X_batch)
loss = loss_fn(y_pred, y_batch)
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
u = torch.unsqueeze(X_test[-1],dim=1)
last_day = model(u)
receipts_npy = np.array([], dtype = np.float32)
model.eval()
def callModel(x):
global receipts_npy
with torch.no_grad():
for _ in range(365):
x = model(x)
val = x.detach().numpy()
receipts_npy = np.append(receipts_npy,val[:,-1,:].flatten().item())
return x
callModel(last_day)
source["type"] = "past"
#Predict future months
date_range = pd.date_range(start='2022-01-01', periods=365, freq='D')
future_source = pd.DataFrame({'# Date': date_range, 'Receipt_Count': receipts_npy, 'type':"future"})
concat_source = pd.concat([source,future_source],axis=0)
chart = get_chart(concat_source)
#Display Chart
st.altair_chart(chart, use_container_width=True)
#Display table
months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
source['# Date'] = pd.to_datetime(source['# Date'], errors='coerce')
source = source.rename(columns={source.columns[0]: 'Month',"Receipt_Count":"Number of receipts"})
past_csv = convert_df(source)
past_table = source.groupby(source['Month'].dt.strftime('%B'))['Number of receipts'].sum().sort_values().to_frame()
past_table = past_table.reindex(months)
future_source['# Date'] = pd.to_datetime(future_source['# Date'], errors='coerce')
future_csv = convert_df(future_source)
future_source = future_source.rename(columns={future_source.columns[0]: 'Month',"Receipt_Count":"Number of receipts"})
future_table = future_source.groupby(source['Month'].dt.strftime('%B'))['Number of receipts'].sum().sort_values().to_frame()
future_table = future_table.reindex(months)
future_table.loc[:, "Number of receipts"] = future_table["Number of receipts"].astype('int32').map('{:,d}'.format)
past_table.loc[:, "Number of receipts"] = past_table["Number of receipts"].map('{:,d}'.format)
st.write("## Monthly scanned receipts for 2021")
st.download_button(
"Press to Download (Daily)",
past_csv,
"file.csv",
"text/csv",
key='download-csv-past'
)
st.table(past_table)
st.write("## Predicted monthly scanned receipts for 2022")
st.download_button(
"Press to Download (Daily)",
future_csv,
"file.csv",
"text/csv",
key='download-csv-future'
)
st.table(future_table)
st.write("## Code")
st.write(
"Find at public [GitHub"
" repository](https://github.com/ProgramComputer/receipts-dlinear)"
)