File size: 3,331 Bytes
4df500f
 
4272611
4df500f
4272611
 
5d072bb
4df500f
0fd6f85
9761264
 
0fd6f85
9761264
4df500f
 
5d072bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244bdb0
5d072bb
1a7f62a
 
 
 
 
 
 
 
 
9761264
 
 
 
 
 
 
 
4df500f
 
 
 
 
 
 
 
9761264
 
 
 
0fd6f85
4df500f
 
213acd5
5d072bb
4df500f
0fd6f85
213acd5
df6e739
cf1597e
 
 
213acd5
4df500f
 
1a7f62a
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 (only those that were used during training)
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('EMI Amount', 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)

# CIBIL score input as a text field
cibil_score = st.text_input('CIBIL Score (Enter a number between 300 and 900)', value='700')

# Convert the CIBIL score input to a numeric value
try:
    cibil_score = int(cibil_score)
except ValueError:
    st.error("Please enter a valid number for CIBIL Score.")

# Set default values for the missing features
dti = 0.0  # Example default value for DTI
revol_bal = 0.0  # Example default value for Revolving Balance
revol_util = 0.0  # Example default value for Revolving Utilization
total_acc = 0  # Example default value for Total Accounts
mort_acc = 0  # Example default value for Mortgage Accounts

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 additional feature (not part of scaling)
input_data['loan_amnt_by_income'] = [loan_amnt_by_income]


input_data['cibil_score'] = cibil_score  
input_data = input_data[num_features + ['loan_amnt_by_income']]  

# Predict using the model
if st.button('Predict'):
    if 300 <= cibil_score <= 900:  # Ensure CIBIL score is within the valid range
        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)
    else:
        st.error("Please enter a CIBIL Score between 300 and 900.")