Spaces:
Sleeping
Sleeping
# import libraries | |
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import pickle | |
with open('svc_grid_best.pkl', 'rb') as file_1: | |
model = pickle.load(file_1) | |
# make functions to return digit for certain text | |
# feature sex | |
def get_sex_id(sex): | |
if sex == 'Male': | |
return 1 | |
elif sex=='Female': | |
return 2 | |
# feature education_level | |
def get_edu_id(education_level): | |
if education_level == 'Graduate School': | |
return 1 | |
elif education_level == 'University': | |
return 2 | |
elif education_level == 'High School': | |
return 3 | |
elif education_level == 'Others': | |
return 4 | |
elif education_level == 'Unknown': | |
return 5 | |
# feature marital_status | |
def get_marital_id(marital_status): | |
if marital_status == 'Married': | |
return 1 | |
elif marital_status == 'Single': | |
return 2 | |
elif marital_status == 'Others': | |
return 3 | |
# feature pay_[i] | |
for i in ['pay_0', 'pay_2', 'pay_3', 'pay_4', 'pay_5', 'pay_6']: | |
def get_pay_id(i): | |
if i == 'No transaction': | |
return -2.0 | |
elif i == 'Paid duly': | |
return -1.0 | |
elif i == 'Payment delay (1 mo)': | |
return 1.0 | |
elif i == 'Payment delay (2 mo)': | |
return 2.0 | |
elif i == 'Payment delay (3 mo)': | |
return 3.0 | |
elif i == 'Payment delay (4 mo)': | |
return 4.0 | |
elif i == 'Payment delay (5 mo)': | |
return 5.0 | |
elif i == 'Payment delay (6 mo)': | |
return 6.0 | |
elif i == 'Payment delay (7 mo)': | |
return 7.0 | |
elif i == 'Payment delay (8 mo)': | |
return 8.0 | |
elif i == 'Payment delay (9 mo)': | |
return 9.0 | |
def run(): | |
# make title | |
st.title('Credit Card Payment Prediction') | |
# make description | |
st.write('This page was made to predict whether the user will pay on time next month or fail to do so (default).') | |
# insert image | |
st.image('https://images.csmonitor.com/csm/2012/11/1130creditcards.jpg?alias=standard_900x600nc', caption='Credit Cards (https://images.csmonitor.com/csm/2012/11/1130creditcards.jpg?alias=standard_900x600nc/Elise Amendola)') | |
# make form | |
with st.form("GC5_form"): | |
# general info | |
# make subheader | |
st.write('## General Information') | |
# define each feature | |
limit_balance = st.number_input('Limit Balance', min_value=0, max_value= 1000000, value=120000) | |
sex = st.selectbox('Sex', ['Male', 'Female'], index=1) | |
education_level = st.selectbox('Education Level', ['Graduate School', 'University', 'High School', 'Others', 'Unknown'], index=2) | |
marital_status = st.selectbox('Marital Status', ['Married', 'Single', 'Others'], index=1) | |
age = st.number_input('Age', min_value=18, max_value= 100, value=25) | |
# make border | |
st.markdown('---') | |
# repayment status | |
# make subheader | |
st.write('## Repayment Status') | |
# define rows | |
row1 = col1, col2, col3 = st.columns(3) | |
row2 = col4, col5, col6 = st.columns(3) | |
# insert features to columns | |
with col1: | |
pay_0 = st.selectbox('September 2005', ['No transaction', 'Paid duly', 'Payment delay (1 mo)', 'Payment delay (2 mo)', 'Payment delay (3 mo)', 'Payment delay (4 mo)', 'Payment delay (5 mo)', 'Payment delay (6 mo)', 'Payment delay (7 mo)', 'Payment delay (8 mo)', 'Payment delay (9 mo)'], index=1) | |
with col2: | |
pay_2 = st.selectbox('August 2005', ['No transaction', 'Paid duly', 'Payment delay (1 mo)', 'Payment delay (2 mo)', 'Payment delay (3 mo)', 'Payment delay (4 mo)', 'Payment delay (5 mo)', 'Payment delay (6 mo)', 'Payment delay (7 mo)', 'Payment delay (8 mo)', 'Payment delay (9 mo)'], index=1) | |
with col3: | |
pay_3 = st.selectbox('July 2005', ['No transaction', 'Paid duly', 'Payment delay (1 mo)', 'Payment delay (2 mo)', 'Payment delay (3 mo)', 'Payment delay (4 mo)', 'Payment delay (5 mo)', 'Payment delay (6 mo)', 'Payment delay (7 mo)', 'Payment delay (8 mo)', 'Payment delay (9 mo)'], index=1) | |
with col4: | |
pay_4 = st.selectbox('June 2005', ['No transaction', 'Paid duly', 'Payment delay (1 mo)', 'Payment delay (2 mo)', 'Payment delay (3 mo)', 'Payment delay (4 mo)', 'Payment delay (5 mo)', 'Payment delay (6 mo)', 'Payment delay (7 mo)', 'Payment delay (8 mo)', 'Payment delay (9 mo)'], index=1) | |
with col5: | |
pay_5 = st.selectbox('May 2005', ['No transaction', 'Paid duly', 'Payment delay (1 mo)', 'Payment delay (2 mo)', 'Payment delay (3 mo)', 'Payment delay (4 mo)', 'Payment delay (5 mo)', 'Payment delay (6 mo)', 'Payment delay (7 mo)', 'Payment delay (8 mo)', 'Payment delay (9 mo)'], index=1) | |
with col6: | |
pay_6 = st.selectbox('April 2005', ['No transaction', 'Paid duly', 'Payment delay (1 mo)', 'Payment delay (2 mo)', 'Payment delay (3 mo)', 'Payment delay (4 mo)', 'Payment delay (5 mo)', 'Payment delay (6 mo)', 'Payment delay (7 mo)', 'Payment delay (8 mo)', 'Payment delay (9 mo)'], index=1) | |
# make tiles | |
for col in row1 + row2: | |
tile = col.container() | |
# make border | |
st.markdown('---') | |
# bill_amt | |
# make subheader | |
st.write('## Amount of Bill Statement') | |
# define rows | |
row3 = col7, col8, col9 = st.columns(3) | |
row4 = col10, col11, col12 = st.columns(3) | |
# insert features to columns | |
with col7: | |
bill_amt_1 = st.number_input('September 2005', min_value = -1000000, max_value = 1000000, value = 4000) | |
with col8: | |
bill_amt_2 = st.number_input('August 2005', min_value = -1000000, max_value = 1000000, value = 4020) | |
with col9: | |
bill_amt_3 = st.number_input('July 2005', min_value = -1000000, max_value = 1000000, value = 4010) | |
with col10: | |
bill_amt_4 = st.number_input('June 2005', min_value = -1000000, max_value = 1000000, value = 4005) | |
with col11: | |
bill_amt_5 = st.number_input('May 2005', min_value = -1000000, max_value = 1000000, value = 4000) | |
with col12: | |
bill_amt_6 = st.number_input('April 2005', min_value = -1000000, max_value = 1000000, value = 4000) | |
# make tiles | |
for col in row3 + row4: | |
tile = col.container() | |
# make border | |
st.markdown('---') | |
# amount of previous payment | |
# make subheader | |
st.write('## Amount of Previous Payment') | |
# define rows | |
row5 = col13, col14, col15 = st.columns(3) | |
row6 = col16, col17, col18 = st.columns(3) | |
# insert features to columns | |
with col13: | |
pay_amt_1 = st.number_input('September 2005', min_value = -1000000, max_value = 1000000, value = 400) | |
with col14: | |
pay_amt_2 = st.number_input('August 2005', min_value = -1000000, max_value = 1000000, value = 400) | |
with col15: | |
pay_amt_3 = st.number_input('July 2005', min_value = -1000000, max_value = 1000000, value = 400) | |
with col16: | |
pay_amt_4 = st.number_input('June 2005', min_value = -1000000, max_value = 1000000, value = 400) | |
with col17: | |
pay_amt_5 = st.number_input('May 2005', min_value = -1000000, max_value = 1000000, value = 400) | |
with col18: | |
pay_amt_6 = st.number_input('April 2005', min_value = -1000000, max_value = 1000000, value = 400) | |
# make tiles | |
for col in row5 + row6: | |
tile = col.container() | |
# make submit button | |
submitted = st.form_submit_button("Submit") | |
# define inference data based on inputted data | |
inf_data = { | |
'limit_balance': limit_balance, | |
'sex': get_sex_id(sex), | |
'education_level': get_edu_id(education_level), | |
'marital_status': get_marital_id(marital_status), | |
'age': age, | |
'pay_0': get_pay_id(pay_0), | |
'pay_2': get_pay_id(pay_2), | |
'pay_3': get_pay_id(pay_3), | |
'pay_4': get_pay_id(pay_4), | |
'pay_5': get_pay_id(pay_5), | |
'pay_6': get_pay_id(pay_6), | |
'bill_amt_1': bill_amt_1, | |
'bill_amt_2': bill_amt_2, | |
'bill_amt_3': bill_amt_3, | |
'bill_amt_4': bill_amt_4, | |
'bill_amt_5': bill_amt_5, | |
'bill_amt_6': bill_amt_6, | |
'pay_amt_1': pay_amt_1, | |
'pay_amt_2':pay_amt_2, | |
'pay_amt_3': pay_amt_3, | |
'pay_amt_4': pay_amt_4, | |
'pay_amt_5': pay_amt_5, | |
'pay_amt_6': pay_amt_6 | |
} | |
# make dataframe for inference data | |
inf_data = pd.DataFrame([inf_data]) | |
# create condition | |
if submitted: | |
# define result using model | |
result= model.predict(inf_data) | |
# print result | |
st.write(f'# Default Payment Next Month: {round(result[0])}') | |
# show balloons after submitting | |
st.balloons() | |
# execute file | |
if __name__ == '__main__': | |
run() |