|
| 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