forked from foxpcteam/AI-Marketer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartanalysis.py
125 lines (103 loc) · 6.12 KB
/
cartanalysis.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
import streamlit as st
import requests
from util import p_title
import time
import json
import pandas as pd
from pycaret.arules import *
from io import StringIO
from pycaret.datasets import get_data
def cartanalysis(nav):
if nav == '💰Cart Analysis':
st.text('')
p_title('💰Cart Analysis')
st.text('')
st.markdown(':white_check_mark:Find out the correlation of different products to set product combination')
st.text('Suggested Data : Invoice No (Unique Identifier of each order ) , Product Name (Product Description)')
if st.button("Get Demo Data") :
# Can be used wherever a "file-like" object is accepted:
data = pd.read_csv('./dataset/rfm.csv')
#check the input of data
st.dataframe(data.head(10))
with st.spinner('Training Model'):
s = setup(data=data,transaction_id = 'InvoiceNo',item_id = 'Description', ignore_items = 'POSTAGE')
try:
model = create_model()
if not model.empty:
model['antecedents'] = [' + '.join(list(i)) for i in list(model['antecedents'])]
model['consequents'] = [' + '.join(list(i)) for i in list(model['consequents'])]
else:
st.write("No relationship could be found in the cart")
model = model[['antecedents', 'consequents', 'lift']]
model = model.sort_values(by='lift', ascending=False)
# if user input cannot be found in the data
except KeyError as e:
print(repr(e))
st.error("Wrong user input! Could not find in the data")
# output
try:
#st.write('[Lift ratio example: If 50% of people who buy A, B will buy C, and in general 25% of people will buy C, the lift ratio will be 50%/25% = 2.\n The larger the ratio, the relationship of A, B leading to C is more significant.]')
model = model.rename(columns={"antecedents":"Cart set (When customer bought...)","consequents":"Predicted Set (they will also buy...)","lift":"When customer buying the Cart set, the possibility of buying the Predicted set is increased ..."})
st.title('Result')
st.dataframe(model,width=2000)
except:
pass
else:
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
# To read file as bytes:
bytes_data = uploaded_file.getvalue()
#st.write(bytes_data)
# To convert to a string based IO:
#stringio = StringIO(uploaded_fil.getvalue().decode("utf-8"))
#st.write(stringio)
# To read file as string:
#string_data = stringio.read()
# Can be used wherever a "file-like" object is accepted:
data = pd.read_csv(uploaded_file)
#check the input of data
st.dataframe(data.head(10))
#Let user input the name of transaction_id & Description
example_InvoiceNo = 'InvoiceNo'
example_Description = 'Description'
example_ignore_items = 'POSTAGE'
InvoiceNo = st.text_input('Input the Invoice ID', example_InvoiceNo)
Description = st.text_input('Input the items ID',example_Description)
ignore_items = st.text_input('Input the ignore items (Seperated by comma) (Optional)',example_ignore_items)
ignore_items = [i.strip() for i in ignore_items.split(',')]
ignore_items = [i for i in ignore_items if i != '']
if st.button('Submit') :
with st.spinner('Training Model'):
s = setup(data=data,transaction_id = InvoiceNo,item_id = Description, ignore_items = ignore_items)
try:
model = create_model()
if not model.empty:
model['antecedents'] = [' + '.join(list(i)) for i in list(model['antecedents'])]
model['consequents'] = [' + '.join(list(i)) for i in list(model['consequents'])]
else:
st.write("No relationship could be found in the cart")
model = model[['antecedents', 'consequents', 'lift']]
model = model.sort_values(by='lift', ascending=False)
# if user input cannot be found in the data
except KeyError as e:
print(repr(e))
st.error("Wrong user input! Could not find in the data")
# output
try:
#st.write('[Lift ratio example: If 50% of people who buy A, B will buy C, and in general 25% of people will buy C, the lift ratio will be 50%/25% = 2.\n The larger the ratio, the relationship of A, B leading to C is more significant.]')
model = model.rename(columns={"antecedents":"Cart set (When customer bought...)","consequents":"Predicted Set (they will also buy...)","lift":"When customer buying the Cart set, the possibility of buying the Predicted set is increased ..."})
st.title('Result')
st.dataframe(model)
@st.cache
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode('utf-8')
csv = convert_df(model)
st.download_button(
label="Download data as CSV",
data=csv,
file_name='cart.csv',
mime='text/csv',
)
except:
pass