-
Notifications
You must be signed in to change notification settings - Fork 0
/
prediction_helper.py
92 lines (73 loc) · 3.46 KB
/
prediction_helper.py
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
89
90
91
92
import joblib
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# Path to the saved model and its components
MODEL_PATH = 'artifacts/model_data.joblib'
# Load the model and its components
model_data = joblib.load(MODEL_PATH)
model = model_data['model']
scaler = model_data['scaler']
features = model_data['features']
cols_to_scale = model_data['cols_to_scale']
def prepare_df(age, income, loan_amount, loan_tenure_months, avg_dpd_per_delinquency,
delinquency_ratio, credit_utilization_ratio, num_open_accounts,
residence_type, loan_purpose, loan_type):
input_data = {
'age': age,
'loan_tenure_months': loan_tenure_months,
'number_of_open_accounts': num_open_accounts,
'credit_utilization_ratio': credit_utilization_ratio,
'loan_to_income': loan_amount / income if income > 0 else 0,
'delinquency_ratio': delinquency_ratio,
'avg_dpd_per_delinquency': avg_dpd_per_delinquency,
'residence_type_Owned': 1 if residence_type == 'Owned' else 0,
'residence_type_Rented': 1 if residence_type == 'Rented' else 0,
'loan_purpose_Education': 1 if loan_purpose == 'Education' else 0,
'loan_purpose_Home': 1 if loan_purpose == 'Home' else 0,
'loan_purpose_Personal': 1 if loan_purpose == 'Personal' else 0,
'loan_type_Unsecured': 1 if loan_type == 'Unsecured' else 0,
# add addtional fields
'number_of_dependants': 1, # Dummy value
'years_at_current_address': 1, # Dummy value
'zipcode': 1, # Dummy value
'sanction_amount': 1, # Dummy value
'processing_fee': 1, # Dummy value
'gst': 1, # Dummy value
'net_disbursement': 1, # Computed dummy value
'principal_outstanding': 1, # Dummy value
'bank_balance_at_application': 1, # Dummy value
'number_of_closed_accounts': 1, # Dummy value
'enquiry_count': 1 # Dummy value
}
df = pd.DataFrame([input_data])
df[cols_to_scale] = scaler.transform(df[cols_to_scale])
df = df[features]
return df
def calculate_credit_score(input_df, base_score=300, scale_lenth=600):
x = np.dot(input_df.values, model.coef_.T) + model.intercept_
default_probability = 1 / (1+np.exp(-x))
non_default_probability = 1 - default_probability
credit_score = base_score + non_default_probability.flatten() * scale_lenth
# Determine the rating category based on the credit score
def get_rating(score):
if 300 <= score < 500:
return 'Poor'
elif 500 <= score < 650:
return 'Average'
elif 650 <= score < 750:
return 'Good'
elif 750 <= score <= 900:
return 'Excellent'
else:
return 'Undefined' # in case of any unexpected score
rating = get_rating(credit_score[0])
return default_probability.flatten()[0], int(credit_score), rating
def predict(age, income, loan_amount, loan_tenure_months, avg_dpd_per_delinquency,
delinquency_ratio, credit_utilization_ratio, num_open_accounts,
residence_type, loan_purpose, loan_type):
input_df = prepare_df(age, income, loan_amount, loan_tenure_months, avg_dpd_per_delinquency,
delinquency_ratio, credit_utilization_ratio, num_open_accounts,
residence_type, loan_purpose, loan_type)
probability, credit_score, rating = calculate_credit_score(input_df)
return probability, credit_score, rating