Skip to content

Commit 44e38e6

Browse files
committed
add files
1 parent 275a6ea commit 44e38e6

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
__pycache__/
33
*.py[cod]
44
*$py.class
5+
.idea
56

67
# C extensions
78
*.so

artifacts/model_data.joblib

4.72 KB
Binary file not shown.

main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import streamlit as st
2+
from prediction_helper import predict # Ensure this is correctly linked to your prediction_helper.py
3+
4+
# Set the page configuration and title
5+
st.set_page_config(page_title="Lauki Finance: Credit Risk Modelling", page_icon="📊")
6+
st.title("Lauki Finance: Credit Risk Modelling")
7+
8+
# Create rows of three columns each
9+
row1 = st.columns(3)
10+
row2 = st.columns(3)
11+
row3 = st.columns(3)
12+
row4 = st.columns(3)
13+
14+
# Assign inputs to the first row with default values
15+
with row1[0]:
16+
age = st.number_input('Age', min_value=18, step=1, max_value=100, value=28)
17+
with row1[1]:
18+
income = st.number_input('Income', min_value=0, value=1200000)
19+
with row1[2]:
20+
loan_amount = st.number_input('Loan Amount', min_value=0, value=2560000)
21+
22+
# Calculate Loan to Income Ratio and display it
23+
loan_to_income_ratio = loan_amount / income if income > 0 else 0
24+
with row2[0]:
25+
st.text("Loan to Income Ratio:")
26+
st.text(f"{loan_to_income_ratio:.2f}") # Display as a text field
27+
28+
# Assign inputs to the remaining controls
29+
with row2[1]:
30+
loan_tenure_months = st.number_input('Loan Tenure (months)', min_value=0, step=1, value=36)
31+
with row2[2]:
32+
avg_dpd_per_delinquency = st.number_input('Avg DPD', min_value=0, value=20)
33+
34+
with row3[0]:
35+
delinquency_ratio = st.number_input('Delinquency Ratio', min_value=0, max_value=100, step=1, value=30)
36+
with row3[1]:
37+
credit_utilization_ratio = st.number_input('Credit Utilization Ratio', min_value=0, max_value=100, step=1, value=30)
38+
with row3[2]:
39+
num_open_accounts = st.number_input('Open Loan Accounts', min_value=1, max_value=4, step=1, value=2)
40+
41+
42+
with row4[0]:
43+
residence_type = st.selectbox('Residence Type', ['Owned', 'Rented', 'Mortgage'])
44+
with row4[1]:
45+
loan_purpose = st.selectbox('Loan Purpose', ['Education', 'Home', 'Auto', 'Personal'])
46+
with row4[2]:
47+
loan_type = st.selectbox('Loan Type', ['Unsecured', 'Secured'])
48+
49+
50+
# Button to calculate risk
51+
if st.button('Calculate Risk'):
52+
probability, credit_score, rating = predict(age, income, loan_amount, loan_tenure_months, avg_dpd_per_delinquency,
53+
delinquency_ratio, credit_utilization_ratio, num_open_accounts,
54+
residence_type, loan_purpose, loan_type)
55+
56+
# Display the results
57+
st.write(f"Deafult Probability: {probability:.2%}")
58+
st.write(f"Credit Score: {credit_score}")
59+
st.write(f"Rating: {rating}")
60+
61+
# Footer
62+
# st.markdown('_Project From Codebasics ML Course_')

prediction_helper.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import joblib
2+
import numpy as np
3+
import pandas as pd
4+
from sklearn.preprocessing import MinMaxScaler
5+
6+
# Path to the saved model and its components
7+
MODEL_PATH = 'artifacts/model_data.joblib'
8+
9+
# Load the model and its components
10+
model_data = joblib.load(MODEL_PATH)
11+
model = model_data['model']
12+
scaler = model_data['scaler']
13+
features = model_data['features']
14+
cols_to_scale = model_data['cols_to_scale']
15+
16+
def prepare_df(age, income, loan_amount, loan_tenure_months, avg_dpd_per_delinquency,
17+
delinquency_ratio, credit_utilization_ratio, num_open_accounts,
18+
residence_type, loan_purpose, loan_type):
19+
20+
input_data = {
21+
'age': age,
22+
'loan_tenure_months': loan_tenure_months,
23+
'number_of_open_accounts': num_open_accounts,
24+
'credit_utilization_ratio': credit_utilization_ratio,
25+
'loan_to_income': loan_amount / income if income > 0 else 0,
26+
'delinquency_ratio': delinquency_ratio,
27+
'avg_dpd_per_delinquency': avg_dpd_per_delinquency,
28+
'residence_type_Owned': 1 if residence_type == 'Owned' else 0,
29+
'residence_type_Rented': 1 if residence_type == 'Rented' else 0,
30+
'loan_purpose_Education': 1 if loan_purpose == 'Education' else 0,
31+
'loan_purpose_Home': 1 if loan_purpose == 'Home' else 0,
32+
'loan_purpose_Personal': 1 if loan_purpose == 'Personal' else 0,
33+
'loan_type_Unsecured': 1 if loan_type == 'Unsecured' else 0,
34+
# add addtional fields
35+
'number_of_dependants': 1, # Dummy value
36+
'years_at_current_address': 1, # Dummy value
37+
'zipcode': 1, # Dummy value
38+
'sanction_amount': 1, # Dummy value
39+
'processing_fee': 1, # Dummy value
40+
'gst': 1, # Dummy value
41+
'net_disbursement': 1, # Computed dummy value
42+
'principal_outstanding': 1, # Dummy value
43+
'bank_balance_at_application': 1, # Dummy value
44+
'number_of_closed_accounts': 1, # Dummy value
45+
'enquiry_count': 1 # Dummy value
46+
}
47+
48+
df = pd.DataFrame([input_data])
49+
50+
df[cols_to_scale] = scaler.transform(df[cols_to_scale])
51+
52+
df = df[features]
53+
54+
return df
55+
56+
57+
58+
def calculate_credit_score(input_df, base_score=300, scale_lenth=600):
59+
x = np.dot(input_df.values, model.coef_.T) + model.intercept_
60+
61+
default_probability = 1 / (1+np.exp(-x))
62+
non_default_probability = 1 - default_probability
63+
64+
credit_score = base_score + non_default_probability.flatten() * scale_lenth
65+
# Determine the rating category based on the credit score
66+
def get_rating(score):
67+
if 300 <= score < 500:
68+
return 'Poor'
69+
elif 500 <= score < 650:
70+
return 'Average'
71+
elif 650 <= score < 750:
72+
return 'Good'
73+
elif 750 <= score <= 900:
74+
return 'Excellent'
75+
else:
76+
return 'Undefined' # in case of any unexpected score
77+
78+
rating = get_rating(credit_score[0])
79+
80+
return default_probability.flatten()[0], int(credit_score), rating
81+
82+
def predict(age, income, loan_amount, loan_tenure_months, avg_dpd_per_delinquency,
83+
delinquency_ratio, credit_utilization_ratio, num_open_accounts,
84+
residence_type, loan_purpose, loan_type):
85+
86+
input_df = prepare_df(age, income, loan_amount, loan_tenure_months, avg_dpd_per_delinquency,
87+
delinquency_ratio, credit_utilization_ratio, num_open_accounts,
88+
residence_type, loan_purpose, loan_type)
89+
90+
probability, credit_score, rating = calculate_credit_score(input_df)
91+
92+
return probability, credit_score, rating

0 commit comments

Comments
 (0)