File size: 982 Bytes
fe8f546
 
cdccaab
fe8f546
 
 
cdccaab
 
 
 
 
 
fe8f546
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdccaab
 
 
 
 
fe8f546
cdccaab
fe8f546
1ac990b
 
 
 
fe8f546
 
 
 
d8c411d
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
import streamlit as st 
import pandas as pd
import matplotlib.pyplot as plt
st.title("Customer Lifetime Value App")

# Read the dataset
data = pd.read_csv('Online Retail.csv', encoding= 'unicode_escape')
st.write(data)

data['InvoiceDate'] = pd.to_datetime(data['InvoiceDate'])
data["value"] = data.UnitPrice * data.Quantity


# Get the user id
user_id = st.selectbox('Select the user id :', data.CustomerID.unique())

# Get the data for the selected user id
user_data = data[data['CustomerID'] == user_id]

# Calculate the CLV
clv = (user_data.UnitPrice * user_data.Quantity).sum()

st.write('Customer lifetime value : ', clv)


# Plot the graphs
st.subheader('Purchase Trend')


gr = user_data.groupby(user_data["InvoiceDate"].dt.floor("30D")).sum()
fig = plt.figure()
ax = fig.add_subplot(1,1,1)

plt.scatter(x=gr.index, y=gr["value"], c=gr["value"]<0)

st.write(fig)





# Risk of Churn
if clv <= 0:
    st.write('Risk of Churn : Yes')
else:
    st.write('Risk of Churn : No')