File size: 2,796 Bytes
4df500f
 
4272611
4df500f
4272611
 
5d072bb
4df500f
 
 
213acd5
4df500f
 
 
5d072bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4df500f
 
 
 
 
 
 
 
 
 
 
 
213acd5
4df500f
 
213acd5
5d072bb
4df500f
213acd5
 
 
4df500f
 
 
5d072bb
 
 
 
 
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
import streamlit as st
import pandas as pd
from joblib import load

# Load the trained model and scaler
model = load('loandefaulter.joblib')
scaler = load('scaler.joblib')

# Define numerical features for scaling
num_features = [
    'loan_amnt', 'int_rate', 'installment', 'annual_inc', 'dti', 'revol_bal', 'revol_util', 'total_acc', 'mort_acc'
]

# Create the Streamlit app
st.set_page_config(page_title='Loan Default Prediction', layout='wide')

# App title and description
st.markdown("""
    <style>
    .title { font-size: 36px; font-weight: bold; color: #2E86C1; }
    .description { font-size: 20px; color: #34495E; }
    .input-container { margin-top: 20px; }
    .slider-container { margin: 10px 0; }
    </style>
    <div class="title">Loan Default Prediction</div>
    <div class="description">Enter the loan details below to get a prediction on whether the loan will be defaulted.</div>
""", unsafe_allow_html=True)

# Input fields with sliders
loan_amnt = st.slider('Loan Amount', min_value=0.0, max_value=1000000.0, step=1000.0, value=10000.0)
int_rate = st.slider('Interest Rate (%)', min_value=0.0, max_value=30.0, step=0.1, value=5.0)
installment = st.slider('Installment', min_value=0.0, max_value=10000.0, step=10.0, value=200.0)
annual_inc = st.slider('Annual Income', min_value=0.0, max_value=1000000.0, step=1000.0, value=50000.0)
dti = st.slider('Debt-to-Income Ratio', min_value=0.0, max_value=100.0, step=0.1, value=15.0)
revol_bal = st.slider('Revolving Balance', min_value=0.0, max_value=500000.0, step=100.0, value=10000.0)
revol_util = st.slider('Revolving Utilization (%)', min_value=0.0, max_value=100.0, step=0.1, value=30.0)
total_acc = st.slider('Total Accounts', min_value=0, max_value=100, step=1, value=10)
mort_acc = st.slider('Mortgage Accounts', min_value=0, max_value=10, step=1, value=1)
loan_amnt_by_income = loan_amnt / (annual_inc + 1)

# Create a DataFrame for the input
input_data = pd.DataFrame({
    'loan_amnt': [loan_amnt],
    'int_rate': [int_rate],
    'installment': [installment],
    'annual_inc': [annual_inc],
    'dti': [dti],
    'revol_bal': [revol_bal],
    'revol_util': [revol_util],
    'total_acc': [total_acc],
    'mort_acc': [mort_acc]
})

# Scale the numerical features that were used to fit the scaler
input_data[num_features] = scaler.transform(input_data[num_features])

# Add the new feature
input_data['loan_amnt_by_income'] = [loan_amnt_by_income]

# Predict using the model
if st.button('Predict'):
    prediction = model.predict(input_data)
    result = "Defaulted" if prediction[0] == 1 else "Not Defaulted"
    color = "red" if prediction[0] == 1 else "green"
    st.markdown(f"""
        <div style="font-size: 24px; color: {color}; font-weight: bold;">Prediction: {result}</div>
    """, unsafe_allow_html=True)