Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
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 Predictor' | |
) | |
def run(): | |
# Membuat Title | |
st.title('Customer Churn Predictor') | |
#Sub header | |
st.subheader('Description for Customer Churn Predicto') | |
# Insert Gambar | |
image = Image.open('music.jpg') | |
st.image(image, caption ='Dengar') | |
#description | |
st.write('The goals of this churn estimator') | |
st.write('Dengar adalah sebuah platform streaming music yang ingin memprediksi customer yang akan churn sehingga meminta bantuan data scientist untuk membuat model dalam melakukan prediksi.') | |
st.write('Dengan model ini diharapkan dengar dapat memprediksi customer churn atau tidak sehingga bisa lebih berfokus pada tujuannya.') | |
st.markdown('---') | |
st.write('This page is created to show the visualization of the dataset') | |
st.markdown('---') | |
#show dataframe | |
st.write('Dataset') | |
dup = pd.read_csv('https://raw.githubusercontent.com/FerdiErs/SQL/main/churn.csv') | |
st.dataframe(dup) | |
#visualization Function | |
def plot_hist(data, title, x_label): | |
#create hist plot | |
fig = plt.figure(figsize=(7, 5)) | |
sns.histplot(data, kde=True, bins=20, edgecolor='black') | |
#Title and Labels | |
st.title(title) | |
st.pyplot(fig) | |
def plot_countplot_with_numbers(data, x, hue, title, palette, figsize=(7, 5)): | |
# Create CountPlot | |
fig = plt.figure(figsize=figsize) | |
g = sns.countplot(x=x, hue=hue, data=data, palette=palette) | |
# Rotate x labels and move legend outside of the plot | |
g.set_xticklabels(g.get_xticklabels(), rotation=45, ha="right") | |
sns.move_legend(g, "upper left", bbox_to_anchor=(1, 1)) | |
# Number in visualization | |
for p in g.patches: | |
height = p.get_height() | |
g.annotate(f'{height}', (p.get_x() + p.get_width() / 2., height), ha='center', va='bottom', fontsize=10) | |
# Title and labels | |
st.title(title) | |
st.pyplot(fig) | |
#Age Distribution | |
plot_hist(data=dup['age'], title='Age distribution', x_label='age') | |
#Time Spent | |
plot_hist(data=dup['avg_time_spent'], title='Time Spent', x_label='avg_time_spent') | |
#pie chart customer region | |
st.write('### Customer Region Distribution') | |
reg = dup.region_category.value_counts() | |
def make_autopct(values): | |
def my_autopct(pct): | |
total = sum(values) | |
val = int(round(pct*total/100.0)) | |
return '{p:.2f}% ({v:d})'.format(p=pct,v=val) | |
return my_autopct | |
# Define a custom color palette | |
colors = plt.cm.tab20c.colors | |
fig = plt.figure(figsize=(5,5)) | |
reg.plot.pie(autopct=make_autopct(reg), startangle=90, colors=colors) | |
plt.title('Customer Region') | |
plt.axis('equal') | |
st.pyplot(fig) | |
#Memberhsip based on Region | |
plot_countplot_with_numbers(x='membership_category',hue='region_category', title='Memberhsip based on Region', data=dup, palette='flare', figsize=(7, 5)) | |
#membuat pie chart churn risk | |
#count churn | |
ch = dup.churn_risk_score.value_counts() | |
# Define a custom color palette | |
colors = plt.cm.Set3.colors | |
# plot the data | |
fig = plt.figure(figsize=(5,5)) | |
ch.plot.pie(autopct=make_autopct(ch), startangle=90, colors=colors) | |
plt.title('Churn Risk') | |
plt.axis('equal') | |
st.pyplot(fig) | |
#churn risk based on gender | |
plot_countplot_with_numbers(data=dup, x='gender', hue='churn_risk_score', title='Churn Risk based on gender', palette='crest', figsize=(7, 5)) | |
#churn risk based on membership | |
plot_countplot_with_numbers(data=dup, x='membership_category', hue='churn_risk_score', title='Churn Risk based on Membership', palette='flare', figsize=(7, 5)) | |
if __name__== '__main__': | |
run() |