import streamlit as st import pandas as pd import joblib # Load the model using joblib model_path = "best_gb_model.joblib" # Change to .joblib extension model = joblib.load(model_path) st.title("Loan Default Prediction") st.write(""" This app predicts the likelihood of a loan default based on your input data. Please fill out the fields below to get a prediction. """) col1, col2, col3 = st.columns(3) with col1: member_id = st.number_input('Member ID (int):', min_value=1, help="Unique identifier for the borrower. (Not used in prediction)") with col2: total_payment = st.number_input('Total Payment (float):', min_value=0.0, help="Total amount paid by the borrower.") with col3: income_to_loan_ratio = st.number_input('Income-to-Loan Ratio (float):', min_value=0.0, help="Ratio of the borrower\'s income to the loan amount.") col4, col5, col6 = st.columns(3) with col4: dti_revol_util = st.number_input('Debt-to-Income Ratio (float):', min_value=0.0, help="Debt-to-income ratio and revolving utilization.") with col5: total_recovery = st.number_input('Total Recovery (float):', min_value=0.0, help="Total amount recovered from the borrower.") with col6: balance_to_credit_ratio = st.number_input('Balance-to-Credit Ratio (float):', min_value=0.0, help="Ratio of the balance to the credit limit.") col7, col8, col9 = st.columns(3) with col7: recoveries_to_balance_ratio = st.number_input('Recoveries to Balance Ratio (float):', min_value=0.0, help="Ratio of recoveries to balance.") with col8: batch_enrolled_to_total_rec_int = st.number_input('Batch Enrolled to Total Recovery Interest Ratio (float):', min_value=0.0, help="Ratio of batch enrolled to total recovery interest.") with col9: loan_amnt_total_rec_int_ratio = st.number_input('Loan Amount to Total Recovery Interest Ratio (float):', min_value=0.0, help="Ratio of loan amount to total recovery interest.") emp_length_missing = st.number_input('Employment Length Missing (int, 0 or 1):', min_value=0, max_value=1, help="Indicates if the employment length is missing (0 or 1).") input_data = pd.DataFrame({ 'total_payment': [total_payment], 'income_to_loan_ratio': [income_to_loan_ratio], 'dti_revol_util': [dti_revol_util], 'total_recovery': [total_recovery], 'balance_to_credit_ratio': [balance_to_credit_ratio], 'recoveries_to_balance_ratio': [recoveries_to_balance_ratio], 'batch_enrolled_to_total_rec_int': [batch_enrolled_to_total_rec_int], 'loan_amnt_total_rec_int_ratio': [loan_amnt_total_rec_int_ratio], 'emp_length_missing': [emp_length_missing] }) st.write('Selected input data (excluding Member ID):') st.write(input_data) if st.button('Predict'): try: prediction = model.predict(input_data) prediction_proba = model.predict_proba(input_data)[:, 1] st.write(f'The predicted loan default status is: {"Default" if prediction[0] == 1 else "No Default"}') st.write(f'Probability of Default: {prediction_proba[0]:.2f}') if prediction[0] == 1: st.error('The model predicts that this loan is likely to default.') else: st.success('The model predicts that this loan is not likely to default.') except Exception as e: st.error(f"Error in prediction: {e}")