rashid01 commited on
Commit
4df500f
·
verified ·
1 Parent(s): 7ad6c58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py CHANGED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import pandas as pd
4
+ from sklearn.preprocessing import StandardScaler
5
+
6
+ # Load the trained model
7
+ with open('loandefaulter.pkl', 'rb') as file:
8
+ model = pickle.load(file)
9
+
10
+ # Initialize the scaler
11
+ scaler = StandardScaler()
12
+
13
+ # Define numerical features for scaling
14
+ num_features = [
15
+ 'loan_amnt', 'int_rate', 'installment', 'annual_inc', 'dti', 'revol_bal', 'revol_util', 'total_acc', 'mort_acc', 'loan_amnt_by_income'
16
+ ]
17
+
18
+ # Create the Streamlit app
19
+ st.title('Loan Default Prediction')
20
+ st.write('Enter the loan details to get a prediction.')
21
+
22
+ # Input fields for user data
23
+ loan_amnt = st.number_input('Loan Amount', min_value=0.0)
24
+ int_rate = st.number_input('Interest Rate', min_value=0.0)
25
+ installment = st.number_input('Installment', min_value=0.0)
26
+ annual_inc = st.number_input('Annual Income', min_value=0.0)
27
+ dti = st.number_input('Debt-to-Income Ratio', min_value=0.0)
28
+ revol_bal = st.number_input('Revolving Balance', min_value=0.0)
29
+ revol_util = st.number_input('Revolving Utilization', min_value=0.0)
30
+ total_acc = st.number_input('Total Accounts', min_value=0)
31
+ mort_acc = st.number_input('Mortgage Accounts', min_value=0)
32
+ loan_amnt_by_income = loan_amnt / (annual_inc + 1)
33
+
34
+ # Create a DataFrame for the input
35
+ input_data = pd.DataFrame({
36
+ 'loan_amnt': [loan_amnt],
37
+ 'int_rate': [int_rate],
38
+ 'installment': [installment],
39
+ 'annual_inc': [annual_inc],
40
+ 'dti': [dti],
41
+ 'revol_bal': [revol_bal],
42
+ 'revol_util': [revol_util],
43
+ 'total_acc': [total_acc],
44
+ 'mort_acc': [mort_acc],
45
+ 'loan_amnt_by_income': [loan_amnt_by_income]
46
+ })
47
+
48
+ # Scale the input data
49
+ input_data[num_features] = scaler.fit_transform(input_data[num_features])
50
+
51
+ # Predict using the model
52
+ if st.button('Predict'):
53
+ prediction = model.predict(input_data)
54
+ st.write(f'Prediction: {"Charged Off" if prediction[0] == 1 else "Not Charged Off"}')