Spaces:
Sleeping
Sleeping
# Import libraries | |
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import pickle | |
import json | |
# Load | |
with open('model_best.pkl', 'rb') as file_1: | |
model = pickle.load(file_1) | |
with open('model_encoder_ord.pkl', 'rb') as file_2: | |
encoder_o = pickle.load(file_2) | |
with open('model_scaler_skew.pkl', 'rb') as file_3: | |
scaler_s = pickle.load(file_3) | |
with open('list_num_cols_s.txt', 'r') as file_4: | |
num_col = json.load(file_4) | |
with open('list_cat_cols_o.txt', 'r') as file_5: | |
cat_col = json.load(file_5) | |
def run(): | |
# Define the features for the options | |
features = ["limit_balance", "sex", "education_level", "marital_status", "age", | |
"pay_1", "pay_2", "pay_3", "pay_4", "pay_5", "pay_6", | |
"bill_amt_1", "bill_amt_2", "bill_amt_3", "bill_amt_4", "bill_amt_5", "bill_amt_6", | |
"pay_amt_1", "pay_amt_2", "pay_amt_3", "pay_amt_4", "pay_amt_5", "pay_amt_6"] | |
# Define the categorical features for the options | |
categorical_features = ["sex", "education_level", "marital_status", "pay_1", "pay_2", "pay_3", "pay_4", "pay_5", "pay_6"] | |
# Define the options for the categorical features | |
options = { | |
"sex": [1, 2], | |
"education_level": [1, 2, 3], | |
"marital_status": [1, 2], | |
"pay_1": [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | |
"pay_2": [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | |
"pay_3": [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | |
"pay_4": [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | |
"pay_5": [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | |
"pay_6": [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | |
} | |
# Displaying the options description | |
st.header('Options description') | |
st.write('1. sex: Gender (1 = male; 2 = female)') | |
st.write('2. education_level: Education Level (1=graduate school, 2=university, 3=high school)') | |
st.write('3. marital_status: Marital status (1 = married; 2 = single)') | |
st.write('4. age: Age (year)') | |
st.write('5. limit_balance: The amount of the given credit (NT dollar)') | |
st.write('6. pay_6 to pay_1: The repayment status in April to September 2005') | |
st.write('7. pay_amt_6 to pay_amt_1: The amount of previous payment in April to September 2005 (NT dollar)') | |
st.write('8. bill_amt_6 to bill_amt_1: The amount of bill statement in April to September 2005 (NT dollar)') | |
# Create a sidebar | |
st.sidebar.title("Prediction Options") | |
st.sidebar.subheader("Enter the values for the features") | |
# Create inputs for the features | |
inputs = {} | |
for feature in features: | |
if feature in categorical_features: | |
inputs[feature] = st.sidebar.selectbox(feature, options[feature]) | |
else: | |
inputs[feature] = st.sidebar.number_input(feature, min_value=0) | |
# Create a button for prediction | |
predict = st.sidebar.button("Predict") | |
# Create a main title | |
st.title("Click the `Predict` button to start") | |
# Display the prediction | |
if predict: | |
# Convert the inputs into a dataframe | |
input_df = pd.DataFrame([inputs]) | |
data_inf_input_num = input_df[num_col] | |
data_inf_input_cat = input_df[cat_col] | |
data_inf_input_num = scaler_s.transform(data_inf_input_num) | |
data_inf_input_cat = encoder_o.transform(data_inf_input_cat) | |
data_inf_input_final = np.concatenate([data_inf_input_num, data_inf_input_cat], axis=1) | |
# Make the prediction | |
prediction = model.predict(data_inf_input_final)[0] | |
# Display the result | |
if prediction == 0: | |
st.success("# The client is not likely to default on their payment next month.") | |
else: | |
st.error("# The client is likely to default on their payment next month.") | |
if __name__ == '__main__': | |
run() | |