|
import streamlit as st |
|
import pandas as pd |
|
import numpy as np |
|
import seaborn as sns |
|
import matplotlib.pyplot as plt |
|
import plotly.express as px |
|
from PIL import Image |
|
|
|
st.set_page_config( |
|
page_title = 'Customer Churn Risk - EDA', |
|
layout = 'wide', |
|
initial_sidebar_state = 'expanded' |
|
) |
|
|
|
def run(): |
|
|
|
st.title('Customer Churn Prediction') |
|
|
|
|
|
st.subheader('EDA for Customer Churn Risk') |
|
|
|
|
|
image = Image.open('churn.jpeg') |
|
st.image(image, caption='Customer Churn') |
|
|
|
|
|
st.write('This page created by **Imam Zarkasie**') |
|
st.write('### Hello!') |
|
st.write('#### The competition of product sales in the e-commerce industry in heating up!') |
|
st.write('##### In this page we can explore customer segmentation, more than that this website provides an ability to predict a customer churn risk.') |
|
|
|
|
|
|
|
st.markdown('---') |
|
|
|
|
|
''' |
|
On this page, the author will do a simple exploration. |
|
The dataset used is the churn dataset. |
|
''' |
|
|
|
|
|
df = pd.read_csv('churn.csv') |
|
st.dataframe(df) |
|
|
|
|
|
df['churn_risk_score'].value_counts().plot(kind='bar') |
|
|
|
|
|
st.write('#### Histogram of Churn') |
|
fig = plt.figure(figsize=(15, 5)) |
|
sns.histplot(df['churn_risk_score'], bins=30, kde=True) |
|
st.pyplot(fig) |
|
|
|
|
|
grouped = df.groupby('membership_category')['churn_risk_score'].value_counts().unstack().fillna(0) |
|
|
|
|
|
fig, ax = plt.subplots(figsize=(8, 3)) |
|
grouped.plot(kind='bar', stacked=False, ax=ax) |
|
|
|
|
|
st.pyplot(fig) |
|
|
|
|
|
grouped = df.groupby('gender')['churn_risk_score'].value_counts().unstack().fillna(0) |
|
|
|
|
|
churn_risk_scores = df['churn_risk_score'].unique() |
|
|
|
|
|
bar_width = 0.35 |
|
|
|
|
|
male_positions = np.arange(len(churn_risk_scores)) |
|
female_positions = male_positions + bar_width |
|
|
|
|
|
fig, ax = plt.subplots(figsize=(8, 4)) |
|
|
|
|
|
ax.bar(male_positions, grouped.loc['M'], width=bar_width, label='Male', color='lightblue') |
|
|
|
|
|
ax.bar(female_positions, grouped.loc['F'], width=bar_width, label='Female', color='pink') |
|
|
|
|
|
ax.set_xticks(male_positions + bar_width/2) |
|
ax.set_xticklabels(churn_risk_scores) |
|
|
|
|
|
ax.set_xlabel('Churn Risk Score') |
|
ax.set_ylabel('Count') |
|
|
|
|
|
ax.set_title('Churn Risk Score by Gender') |
|
|
|
|
|
ax.legend() |
|
|
|
|
|
ax.grid(True, axis='y', linestyle='--', alpha=0.5) |
|
|
|
|
|
st.pyplot(fig) |
|
|
|
|
|
st.write('#### Histogram based on input user') |
|
pilihan = st.selectbox('Pilih column : ', ('avg_time_spent', 'avg_transaction_value', 'avg_frequency_login_days', 'points_in_wallet')) |
|
fig = plt.figure(figsize=(15, 5)) |
|
sns.histplot(df[pilihan], bins=30, kde=True) |
|
st.pyplot(fig) |
|
|
|
|
|
st.write('#### Plotly Plot - points_in_wallet VS avg_transaction_value') |
|
fig = px.scatter(df, x='points_in_wallet', y='avg_transaction_value') |
|
st.plotly_chart(fig) |
|
|
|
if __name__=='__main__': |
|
run() |