File size: 6,179 Bytes
b33b5ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cef64d3
b33b5ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import numpy as np 
import joblib
import streamlit as st

def predict(new_customer_obj, model_pipeline):
    new_cust_df = pd.DataFrame([new_customer_obj])
    st.dataframe(new_cust_df)
    st.write(new_cust_df.info())
    
    user1_prediction = model_pipeline.predict_proba(new_cust_df)[:, 1]
    label = model_pipeline.predict(new_cust_df)[0]
    return user1_prediction, label

def run():
    # load pipeline
    model_pipeline = joblib.load("./src/logistic_regression_pipeline.pkl")
    
    # fetch dataset 
    # data = pd.read_excel('./src/default_of_credit_card_clients.xls', index_col=0)
    # data.columns = data.iloc[0]
    # data = data.drop(data.index[0])
    
    # form creation
    with st.form(key="prediction_form"):
        limit_balance = st.number_input(label='limit balance', value=0, step=1, min_value=0)
        
        # SEX
        sex_options = {
            "Male": "1",
            "Female": "2"
        }
        sex_label = st.selectbox(
            "Sex", 
            options=list(sex_options.keys()), 
            index=None
        )
        
        # EDUCATION
        education_options = {
            "Graduate School": "1",
            "University": "2",
            "High School": "3", 
            "Others": "4"
        }
        education_label = st.selectbox(
            "Education", 
            options=list(education_options.keys()), 
            index=None
        )
            
        # MARITAL STATUS
        marital_options = {
            "Married": "1",
            "Single": "2",
            "Others": "3"
        }
        maritial_label = st.selectbox(
            "Martial Status", 
            options=list(marital_options.keys()),
            index=None
        )
        
        age = st.number_input("Age", value=1, step=1, min_value=1)
        
        st.write(" === Customer Credit Payment History === ")
    
        pay_options = {
                    "none": "0",
                    "pay duly": "-1",
                    "payment delay for 1 month": "1",
                    "payment delay for 2 month": "2",
                    "payment delay for 3 month": "3",
                    "payment delay for 4 month": "4",
                    "payment delay for 5 month": "5",
                    "payment delay for 6 month": "6",
                    "payment delay for 7 month": "7",
                    "payment delay for 8 month": "8",
                    "payment delay for 9 month and above": "9"
                }
        
        pay_4_label = st.selectbox(
            "June 2005", 
            options=list(pay_options.keys()),
        )
        pay_3_label = st.selectbox(
            "July 2005", 
            options=list(pay_options.keys()),
        )
        pay_2_label = st.selectbox(
            "August 2005", 
            options=list(pay_options.keys()),
        )
        pay_0_label = st.selectbox(
            "September 2005", 
            options=list(pay_options.keys()),
        )
        
        st.write(" === Amount of Bill Statement ===")
        
        bill_amt_4 = st.number_input("June 2005", value=0, step=1, help="can be minus. ex: -123456")
        bill_amt_3 = st.number_input("July 2005", value=0, step=1, help="can be minus. ex: -123456")
        bill_amt_2 = st.number_input("August 2005", value=0, step=1, help="can be minus. ex: -123456")
        bill_amt_1 = st.number_input("September 2005", value=0, step=1, help="can be minus. ex: -123456")
        
        st.write(" === Amount of Previous Statement ===")
        
        pay_amt_4 = st.number_input("June 2005", step=1, min_value=0, help="minimum 0")
        pay_amt_3 = st.number_input("July 2005", step=1, min_value=0, help="minimum 0")
        pay_amt_2 = st.number_input("August 2005", step=1, min_value=0, help="minimum 0")
        pay_amt_1 = st.number_input("September 2005", step=1, min_value=0, help="minimum 0")
        
        submitted = st.form_submit_button('Predict')
        
        if submitted:
            # error handling
            if sex_label == None:
                st.error('Sex cannot be empty')
                st.stop()
                
            if education_label == None:
                st.error('Education cannot be empty')
                st.stop()
                
            if maritial_label == None:
                st.error('Marital Status cannot be empty')
                st.stop()
            
            sex = sex_options[sex_label]
            education = education_options[education_label]
            marital_status = marital_options[maritial_label]
            
            pay_0 = pay_options[pay_0_label]
            pay_2 = pay_options[pay_2_label]
            pay_3 = pay_options[pay_3_label]
            pay_4 = pay_options[pay_4_label]
        
            new_customer = {
                "LIMIT_BAL": limit_balance,
                "SEX": sex,            # 1 = male, 2 = female
                "EDUCATION": education,      # 1 = graduate, 2 = university, 3 = high school
                "MARRIAGE": marital_status,       # 1 = married, 2 = single
                "AGE": age,
                "PAY_0": pay_0,
                "PAY_2": pay_2,
                "PAY_3": pay_3,
                "PAY_4": pay_4,
                "BILL_AMT1": bill_amt_1,
                "BILL_AMT2": bill_amt_2,
                "BILL_AMT3": bill_amt_3,
                "BILL_AMT4": bill_amt_4,
                "PAY_AMT1": pay_amt_1,
                "PAY_AMT2": pay_amt_2,
                "PAY_AMT3": pay_amt_3,
                "PAY_AMT4": pay_amt_4
            }
            
            proba_default,predict_label = predict(new_customer, model_pipeline)
            
            proba_label = "Not Default"
            if predict_label == 1:
                proba_label = "Default"
            
            st.write(f"""
                     proba_default = {proba_default}
                     prediction of default payment next month \n
                     prediction probability ={(proba_default * 100)[0]:.3f}% \n
                     prediction label = {proba_label}
                     """)
            

if __name__ == '__main__':
    run()