-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruler.py
381 lines (285 loc) · 11.4 KB
/
ruler.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import base64
import pandas as pd
import streamlit as st
from pycaret.arules import *
st.set_page_config(
page_title="Ruler", page_icon="📏", layout="centered", initial_sidebar_state="auto"
)
st.title("Ruler 📏")
"""
Using machine learning, Ruler will identify underlying relationtips between items and generate rules for them automagically 😎.
For more info about rule learning, see Association [Rule Learning](https://en.wikipedia.org/wiki/Association_rule_learning).
"""
st.markdown("---")
hints = st.empty()
# load data
@st.cache(allow_output_mutation=True, show_spinner=False)
def load_data(data):
"""
try to load data in csv using pandas read_csv method, otherwise try in excel
"""
try:
return pd.read_csv(data)
except:
return pd.read_excel(data)
upload = st.file_uploader("upload your data in csv or xlsx", type=["csv", "xlsx"])
if upload is None:
col1, col2, col3 = st.columns(3)
with col2:
st.write("*or use the sample data below*")
with st.expander("Data", expanded=True):
with st.spinner("Epicly loading data..."):
try:
data = load_data(upload)
# data = data.dropna(inplace=True)
data
except:
data = load_data("data.csv")
# data = data.dropna(inplace=True)
data
st.write(f"###### {data.shape[0]} rows and {data.shape[1]} columns")
# label columns
###########################################################
st.title("Parameter Selection")
st.subheader("What's what?")
col1, col2, col3, col4 = st.columns(4)
with col1:
item = st.selectbox("Item description column", options=(data.columns), index=2)
item_hint = st.empty()
try:
st.write(f"* {data[item].nunique()} unique items")
except:
pass
with col2:
tx = st.selectbox("Transaction column", options=(data.columns))
tx_hint = st.empty()
try:
st.write(f"* {data[tx].nunique()} unique transactions")
st.write(f"* {data[item].count()/data[tx].nunique():.1f} avg items/transaction")
except:
pass
with col3:
confidence = st.number_input(
"Confidence greater/equal to", value=0.85, min_value=0.0, max_value=1.0
)
confidence_hint = st.empty()
with col4:
cols = data.columns.tolist()
cols.insert(0, "None")
date = st.selectbox("Date column (optional)", options=cols, index=5)
date_hint = st.empty()
@st.cache(allow_output_mutation=True)
def to_datetime(column):
"""
convert a column in a pandas dataframe to a pandas datetime format
"""
data[column] = pd.to_datetime(data[column])
return data[column]
if date != "None":
try:
to_datetime(date)
f"""
* {data[date].min()}
to
* {data[date].max()}
"""
except:
st.warning(
"⚠️ Smol oopsie, items in this column are in an invalid date format. If you don't have a dates, please select 'None'"
)
if st.checkbox("Any items you want to ignore?"):
ignore = st.multiselect("Any items you want to ignore?", options=data[item])
else:
ignore = None
ignore_hint = st.empty()
st.markdown("---")
get_rules = st.button("Get rules", key="rules")
if 'rules' not in st.session_state:
st.session_state.key = False
@st.cache(show_spinner=False)
def model_rules():
"""
combine set up and create_model in one cached function. for some reason pycaret's built in combined function to do this (get_rules) doesn't work in streamlit
"""
rule_setup = setup(data=data, transaction_id=tx, item_id=item, ignore_items=ignore)
rules = create_model(threshold=confidence)
return rules
# session state for get_rules button
###########################################################
if get_rules or st.session_state.rules:
with st.spinner("Ruling the rules..."):
rules = model_rules()
def format_rules(df):
"""
filter a few columns and sort by confidence
"""
df = df.filter(items=["antecedents", "consequents", "confidence", "support"])
df.sort_values(by=["confidence"], ascending=False)
# change from frozenset{} to []
# slight modification of https://discuss.streamlit.io/t/problem-with-how-a-df-is-being-displayed/7218/4
# when using tuple() there was a weird comma like ['item',]
df = df.applymap(lambda x: list(x) if isinstance(x, frozenset) else x)
# this regex is only needed if the frozenset part is in the dataframe as a string
# like if you save and read using pandas' to_csv and thenr read_csv
# rules["antecedents"] = rules["antecedents"].str.extract(r"\{(.*?)\}")
# rules["consequents"] = rules["consequents"].str.extract(r"\{(.*?)\}")
return df
if get_rules or st.session_state.rules:
st.title("Results 📋")
download_link = st.empty()
st.markdown("---")
st.write(f"Number of rules = {len(rules)}")
rules = format_rules(df=rules)
table_hint = st.empty()
st.table(rules)
# generate data summary for optional download
# summary = pd.DataFrame(
# {
# "unique items": data[item].nunique(),
# "unique transactions": data[tx].nunique(),
# "avg items/transaction": f"{data[item].count() / data[tx].nunique():.1f}",
# "date range": f"{data[date].min()} to {data[date].max()}",
# "item column used": item,
# "transaction column used": tx,
# "confidence used": confidence,
# "number of rules": len(rules),
# },
# index=[0],
# )
# ^ horizonal didn't look good
if date != "None":
summary = pd.DataFrame(
{
"Feature": [
"unique items",
"unique transactions",
"avg items/transaction",
"date range",
"item column used",
"transaction column used",
"confidence used",
"number of rules",
],
"Info": [
data[item].nunique(),
data[tx].nunique(),
f"{data[item].count() / data[tx].nunique():.1f}",
f"{data[date].min()} to {data[date].max()}",
item,
tx,
confidence,
len(rules),
],
}
)
else:
summary = pd.DataFrame(
{
"Feature": [
"unique items",
"unique transactions",
"avg items/transaction",
"date range",
"item column used",
"transaction column used",
"confidence used",
"number of rules",
],
"Info": [
data[item].nunique(),
data[tx].nunique(),
f"{data[item].count() / data[tx].nunique():.1f}",
"no date column selected",
item,
tx,
confidence,
len(rules),
],
}
)
# st.write(type(rules))
# with pd.ExcelWriter("ruler_report.xlsx", engine="xlsxwriter") as writer:
# data.to_excel(writer, sheet_name="input_data", index=False)
# summary.to_excel(writer, sheet_name="summary_of_stats", index=False)
# rules.to_excel(writer, sheet_name="rules", index=False)
# ModuleNotFoundError: No module named 'openpyxl'
# had to pip install openpyxl for this to work
# also
# ModuleNotFoundError: No module named 'xlsxwriter'
# pip install xlsxwriter
# gave up, too many errors :(
# for download_link
def filedownload(data, text="Download as CSV", file_name="data.csv"):
csv = data.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # strings <-> bytes conversions
href = f'<a href="data:file/csv;base64,{b64}" download={file_name}>{text}</a>'
return href
# attempted modification to download the excel file with all the sheets
# def filedownload(data, text="Download as XLSX", file_name="data.csv"):
# excel = pd.read_excel(data)
# b64 = base64.b64encode(excel.encode()).decode() # strings <-> bytes conversions
# href = f'<a href="data:file/excel;base64,{b64}" download={file_name}>{text}</a>'
# return href
# download_link.markdown(
# filedownload(data="ruler_report.xlsx", file_name="ruler_report.xlsx"),
# unsafe_allow_html=True,
# )
# creating download links for rules, input data and the summary
# had to resort to this since couldn't get excel thing working
with download_link.container():
col1, col2, col3 = st.columns(3)
with col1:
st.markdown(
filedownload(data=rules, file_name="rules.csv", text="Download rules"),
unsafe_allow_html=True,
)
st.write("###### The generated rules you see below")
with col2:
st.markdown(
filedownload(
data=summary, file_name="summary.csv", text="Download summary"
),
unsafe_allow_html=True,
)
st.write("###### Stats of input data, rules and settings used")
with col3:
st.markdown(
filedownload(
data=data, file_name="rules_report.csv", text="Download input data"
),
unsafe_allow_html=True,
)
st.write("###### Input data")
# hints
###########################################################
if hints.checkbox("Click here to show hints 😊"):
tx_hint.info(
"###### **💡 Hint**: unique identifier, like invoice number that shows items per transaction"
)
item_hint.info(
"###### **💡 Hint**: contains the names of the items you want to find rules for"
)
confidence_hint.info("###### **💡 Hint**: filter rules by confidence")
ignore_hint.info(
"###### **💡 Hint**: things in your item list that might not be useful like delivery charges"
)
if get_rules or session_state.rules:
st.markdown(
table_hint.info(
"""
###### **💡 Hint:**
* ###### If **antecedents** are A, B and **consequents** are C - it means people who buy A and B also frequently buy C
* ###### **Confidence** is an indication of how often the rule has been found to be true
* ###### **Support** is an indication of how frequently the itemset appears in the dataset
"""
)
)
# hide hamburger menu
# https://discuss.streamlit.io/t/remove-made-with-streamlit-from-bottom-of-app/1370/2
# https://github.com/streamlit/streamlit/issues/395#issuecomment-579526417
hide_menu_style = """
<style>
#MainMenu {visibility: hidden;}
</style>
"""
st.markdown(hide_menu_style, unsafe_allow_html=True)