Spaces:
Runtime error
Runtime error
Download src/prediction.py from Heizsenberg/analytic-data-efficiency: direct link, hf CLI and curl.
- Browser
- Download file 6.18 kB
-
https://huggingface.co/spaces/Heizsenberg/analytic-data-efficiency/resolve/main/src/prediction.py
- Command line
-
hf download hf://spaces/Heizsenberg/analytic-data-efficiency/src/prediction.py
-
curl -L -o prediction.py https://huggingface.co/spaces/Heizsenberg/analytic-data-efficiency/resolve/main/src/prediction.py
6.18 kB
| 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() |