File size: 4,013 Bytes
68d3508
 
 
b6fc0c1
68d3508
 
 
 
 
 
 
 
 
 
 
 
0a7a566
68d3508
 
 
 
 
 
 
0a7a566
 
68d3508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7a566
68d3508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7a566
68d3508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7a566
68d3508
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st 
import pandas as pd 
import seaborn as sns
import matplotlib.pyplot as plt 
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 Predictor') 

    # Insert Gambar 
    image = Image.open('music.jpg')
    st.image(image, caption ='Dengar')

    #description 
    st.write('The goals of this churn estimator')
    st.write('Dengar is a music streaming platform that ask data scientist to predict will the customer churn')
    st.write('With this model we hope Dengar will be more focused with their goals')
    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')
    st.write('We can see that dengar had a distribution of age from 10-60')

    #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)
    st.write('We can see that dengar had 3 region with the most users from town')

     #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)
    st.write('We can see from the data that most users in Dengar will churn')
    

    #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()