File size: 16,062 Bytes
604e735 |
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 |
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from PIL import Image
import datetime
import streamlit as st
#####################################################################################################################################
st.set_page_config(layout='wide')
# Sidebar: Image + main info on dataset
def data_subset(data, beginning='2010-12-01', end='2011-12-09'):
beginning = pd.to_datetime(beginning)
end = pd.to_datetime(end)
# Subsetting
data = data[(data['InvoiceDate'] >= beginning) & (data['InvoiceDate'] <= end)]
return data
# Loading datasets
df_info = pd.read_csv('static/customer_info.csv')
df_info['InvoiceDate'] = pd.to_datetime(df_info['InvoiceDate'])
with st.sidebar:
col1, col2, col3 = st.columns(3)
with col2:
random_image = Image.open('static/logo_random.png')
st.image(random_image)
# Showing top products
if st.checkbox('Check to see top products sold in a selected timeframe'):
start = st.date_input('Input beginning of the wanted timeframe', datetime.date(2010, 12, 1),
min_value=datetime.date(2010, 12, 1), max_value=datetime.date(2011, 12, 9), key=1)
end = st.date_input('Input beginning of the wanted timeframe', datetime.date(2011, 12, 9),
min_value=start, max_value=datetime.date(2011, 12, 9), key=2)
df_top_products = df_info.copy()
df_subset_products = data_subset(df_top_products, start, end)
df_subset_products = df_top_products.groupby('Description')['Quantity'].sum()
number_chosen_products = st.number_input('How many top products sold do you want to see?', value=5)
df_subset_products_top = pd.DataFrame(df_top_products.sort_values(by='Quantity', ascending=False)).iloc[:number_chosen_products,:]
df_subset_products_top = df_subset_products_top[['Description', 'Quantity']]
st.dataframe(df_subset_products_top)
# Showing most recent clients
if st.checkbox('Check to see the most recent customers in a selected timeframe'):
start_clts = st.date_input('Input beginning of the wanted timeframe', datetime.date(2010, 12, 1),
min_value=datetime.date(2010, 12, 1), max_value=datetime.date(2011, 12, 9), key=3)
end_clts = st.date_input('Input beginning of the wanted timeframe', datetime.date(2011, 12, 9),
min_value=start_clts, max_value=datetime.date(2011, 12, 9), key=4)
df_recent_customers = df_info.copy()
df_subset_recent_customers = data_subset(df_recent_customers, start_clts, end_clts)
df_subset_recent_customers = df_subset_recent_customers.groupby('CustomerID')['Recency'].min()
number_chosen_recency = st.number_input('How many recent customers do you want to see?', value=5)
df_subset_recent_customers_top = pd.DataFrame(df_subset_recent_customers.sort_values()).iloc[:number_chosen_recency,:]
st.dataframe(df_subset_recent_customers_top)
# Showing most prolific customers
if st.checkbox('Check to see the top customers in a selected timeframe'):
start_top = st.date_input('Input beginning of the wanted timeframe', datetime.date(2010, 12, 1),
min_value=datetime.date(2010, 12, 1), max_value=datetime.date(2011, 12, 9), key=5)
end_top = st.date_input('Input beginning of the wanted timeframe', datetime.date(2011, 12, 9),
min_value=start_top, max_value=datetime.date(2011, 12, 9), key=6)
df_top_customers = df_info.copy()
df_subset_top_customers = data_subset(df_top_customers, start_top, end_top)
df_subset_top_customers = df_subset_top_customers.groupby('CustomerID')['Monetary'].sum()
number_chosen_top_clts = st.number_input('How many top customers do you want to see?', value=5)
df_subset_top_customers_top = pd.DataFrame(df_subset_top_customers.sort_values(ascending=False)).iloc[:number_chosen_top_clts,:]
st.dataframe(df_subset_top_customers_top)
#####################################################################################################################################
st.title('E-commerce: client dashboard')
st.write("---")
# Loading dataset
df_info_customer = df_info.copy()
customer_id_default = int(df_info_customer['CustomerID'].min())
# We choose a CustomerID
st.number_input('CustomerID', min_value=customer_id_default, value=customer_id_default, step=1, format="%d", key='customer_id')
customer_id = st.session_state.customer_id
if customer_id not in df_info_customer['CustomerID'].values:
st.write('This CustomerID is not available right now, please find another.')
else:
start_info = st.date_input('Input beginning of the wanted timeframe', datetime.date(2010, 12, 1),
min_value=datetime.date(2010, 12, 1), max_value=datetime.date(2011, 12, 9), key=7)
end_info = st.date_input('Input beginning of the wanted timeframe', datetime.date(2011, 12, 9),
min_value=start_info, max_value=datetime.date(2011, 12, 9), key=8)
df_subset_info_customer = data_subset(df_info_customer, start_info, end_info)
# Main info (recency, number of orders, how much the customer spent)
df_subset_info_customer = df_subset_info_customer[df_subset_info_customer['CustomerID'] == customer_id]
df_main_info = df_subset_info_customer.groupby('CustomerID').agg(Recency=('Recency', 'min'), NbOrder=('NbOrder', 'max'), MonetaryTotal=('Monetary', 'sum'))
# GroupBy to get the mean value of each order for the customer
df_mean_order = df_subset_info_customer.groupby(['InvoiceNo', 'CustomerID']).agg(TotalOrderValue=('Monetary', 'sum'))
df_mean_order = df_mean_order.groupby('CustomerID').agg(MeanOrderValue=('TotalOrderValue', 'mean'))
# GroupBy to get the most bought product and its quantity
df_product_clts = pd.DataFrame(df_info.groupby(['CustomerID','Description'])['Quantity'].sum())
df_product_clts = df_product_clts.reset_index()
df_product_clts = df_product_clts[df_product_clts['CustomerID'] == customer_id]
ids, values = df_product_clts.groupby('CustomerID')['Quantity'].max().index, df_product_clts.groupby('CustomerID')['Quantity'].max().values
df_product_clts = df_product_clts[(df_product_clts['CustomerID'] == ids[0]) & (df_product_clts['Quantity'] == values[0])]
# Now we create the columns we want
df_main_info['MeanOrderValue'] = df_mean_order['MeanOrderValue'].values[0]
df_main_info['MostOrderedProduct'] = df_product_clts['Description'].values[0]
df_main_info['MostOrderedProductQuantity'] = df_product_clts['Quantity'].values[0]
# We can show it now that it's complete
st.dataframe(df_main_info)
st.write("---")
#####################################################################################################################################
st.subheader('Similarity between customers:')
with st.expander('Choose a number of similar customers to compare:'):
if st.checkbox('Only similar customers:'):
options_similar = ['Recency', 'NbOrder', 'MonetaryTotal', 'MeanOrderValue']
option_similar = st.selectbox('Choose a feature to plot:', tuple(options_similar))
df_similar_customer = df_info.copy()
# Main info (recency, number of orders, how much the customer spent)
df_similar_customer_grouped = df_similar_customer.groupby('CustomerID').agg(Recency=('Recency', 'min'), NbOrder=('NbOrder', 'max'), MonetaryTotal=('Monetary', 'sum'))
# GroupBy to get the mean value of each order for the customer
df_mean_order_similar = df_similar_customer.groupby(['InvoiceNo', 'CustomerID']).agg(TotalOrderValue=('Monetary', 'sum'))
df_mean_order_similar = df_mean_order_similar.groupby('CustomerID').agg(MeanOrderValue=('TotalOrderValue', 'mean'))
# Now we create the column we want
df_similar_customer_grouped['MeanOrderValue'] = df_mean_order_similar['MeanOrderValue'].values
# We select the client
df_similar_customer_grouped = df_similar_customer_grouped.reset_index()
df_selected_clt = df_similar_customer_grouped[df_similar_customer_grouped['CustomerID'] == customer_id]
# We calculate distances (euclidean)
distances = []
for i in range(df_similar_customer_grouped.shape[0]):
distance = np.linalg.norm(df_similar_customer_grouped.drop('CustomerID', axis=1).values[i] - df_selected_clt.drop('CustomerID', axis=1).values)
distances.append(distance)
n_neighbors = st.slider("Number of similar customers:", min_value=5, max_value=30, value=10)
neighbors = sorted(distances)[:n_neighbors]
# We get the indices of the similar customers
indices_neighbors = []
for i in range(len(neighbors)):
indices_neighbors.append(distances.index(neighbors[i]))
df_neighbors_selected = df_similar_customer_grouped.iloc[indices_neighbors, :]
fig2, ax = plt.subplots()
ax.set_xlabel('Customers', fontsize=17)
ax.set_ylabel(option_similar, fontsize=17)
ax.axhline(y=df_selected_clt[option_similar].values, color='r', label='axhline - full height')
ax = plt.boxplot(df_neighbors_selected[option_similar], showfliers=False)
st.pyplot(fig2)
if st.checkbox('Compare to all customers:'):
options_all = ['Recency', 'NbOrder', 'MonetaryTotal', 'MeanOrderValue']
option_all = st.selectbox('Choose a feature to plot:', tuple(options_all))
df_all_customer = df_info.copy()
# Main info (recency, number of orders, how much the customer spent)
df_all_customer_grouped = df_all_customer.groupby('CustomerID').agg(Recency=('Recency', 'min'), NbOrder=('NbOrder', 'max'), MonetaryTotal=('Monetary', 'sum'))
# GroupBy to get the mean value of each order for the customer
df_mean_order_all = df_all_customer.groupby(['InvoiceNo', 'CustomerID']).agg(TotalOrderValue=('Monetary', 'sum'))
df_mean_order_all = df_mean_order_all.groupby('CustomerID').agg(MeanOrderValue=('TotalOrderValue', 'mean'))
# Now we create the column we want
df_all_customer_grouped['MeanOrderValue'] = df_mean_order_all['MeanOrderValue'].values
# We select the client
df_selected_clt_all = df_all_customer_grouped.reset_index()
df_selected_clt_all = df_selected_clt_all[df_selected_clt_all['CustomerID'] == customer_id]
# We calculate distances (euclidean)
distances = []
for i in range(df_all_customer_grouped.shape[0]):
distance = np.linalg.norm(df_all_customer_grouped.values[i] - df_selected_clt_all.drop('CustomerID', axis=1).values)
distances.append(distance)
fig2, ax = plt.subplots()
ax.set_xlabel('Customers', fontsize=17)
ax.set_ylabel(option_all, fontsize=17)
ax.axhline(y=df_selected_clt_all[option_all].values, color='r', label='axhline - full height')
ax = plt.boxplot(df_all_customer_grouped[option_all], showfliers=False)
st.pyplot(fig2)
st.write("---")
#####################################################################################################################################
st.subheader('Barplot of top selected products in the selected timeframe:')
with st.expander('Select to choose how many top products you want to see and in which timeframe'):
start_product_date = st.date_input('Input beginning of the wanted timeframe', datetime.date(2010, 12, 1),
min_value=datetime.date(2010, 12, 1), max_value=datetime.date(2011, 12, 9), key=9)
end_product_date = st.date_input('Input beginning of the wanted timeframe', datetime.date(2011, 12, 9),
min_value=start_product_date, max_value=datetime.date(2011, 12, 9), key=10)
df_top_products_plot = df_info.copy()
df_subset_products = data_subset(df_top_products_plot, start_product_date, end_product_date)
start_product, end_product = st.select_slider('Select a range of top product', options=[x for x in range(1, 21)], value=(1, 10))
df_subset_products = df_subset_products.groupby('Description')['Quantity'].sum()
df_subset_products = df_subset_products.reset_index()
df_slider_products = df_subset_products.sort_values(by='Quantity', ascending=False)
df_slider_products = df_slider_products.iloc[start_product-1:end_product, :]
fig, ax = plt.subplots()
bars = plt.barh(y=df_slider_products['Description'], width=df_slider_products['Quantity'], color=['darkmagenta', 'darkblue', 'darkgreen', 'darkred', 'darkgrey', 'darkorange'])
ax.bar_label(bars)
ax = plt.gca().invert_yaxis()
st.subheader('Selected top products:')
st.pyplot(fig)
st.write("---")
#####################################################################################################################################
st.subheader('Barplot of sales:')
with st.expander('Select to choose the periodicity:'):
options_similar = ['Months', 'Days', 'Hours']
option_similar = st.selectbox('Choose a periodicity:', tuple(options_similar))
if option_similar == 'Months':
df_months = df_info.copy()
df_months = df_months.merge(pd.DataFrame(df_months.groupby('CustomerID')['Monetary'].sum()), on='CustomerID')
df_months['Periodicity'] = pd.DatetimeIndex(df_months['InvoiceDate']).month
df_months = df_months.sort_values('Recency')
df_months = df_months.drop_duplicates(subset='CustomerID')
fig1, ax1 = plt.subplots()
ax1 = sns.barplot(x=df_months['Periodicity'], y=df_months['Monetary_y'], errorbar=None)
plt.title('Sales per Months')
plt.xlabel('Periodicity: Months')
plt.ylabel('TotalOrderValue')
st.pyplot(fig1)
elif option_similar == 'Days':
df_days = df_info.copy()
df_days = df_days.merge(pd.DataFrame(df_days.groupby('CustomerID')['Monetary'].sum()), on='CustomerID')
df_days['Periodicity'] = pd.DatetimeIndex(df_days['InvoiceDate']).day
df_days = df_days.sort_values('Recency')
df_days = df_days.drop_duplicates(subset='CustomerID')
fig2, ax2 = plt.subplots()
ax2 = sns.barplot(x=df_days['Periodicity'], y=df_days['Monetary_y'], errorbar=None)
plt.title('Sales per Days')
plt.xlabel('Periodicity: Days')
plt.xticks(rotation=90)
plt.ylabel('TotalOrderValue')
st.pyplot(fig2)
elif option_similar == 'Hours':
df_hours = df_info.copy()
df_hours = df_hours.merge(pd.DataFrame(df_hours.groupby('CustomerID')['Monetary'].sum()), on='CustomerID')
df_hours['Periodicity'] = pd.DatetimeIndex(df_hours['InvoiceDate']).hour
df_hours = df_hours.sort_values('Recency')
df_hours = df_hours.drop_duplicates(subset='CustomerID')
fig3, ax3 = plt.subplots()
ax3 = sns.barplot(x=df_hours['Periodicity'], y=df_hours['Monetary_y'], errorbar=None)
plt.title('Sales per Hours')
plt.xlabel('Periodicity: Hours')
plt.ylabel('TotalOrderValue')
st.pyplot(fig3)
st.write("---")
#####################################################################################################################################
col1, col2, col3, col4, col5 = st.columns(5)
with col5:
logo_artefact = Image.open('static/logo_artefact.png')
st.image(logo_artefact) |