|
import pandas as pd
|
|
import os
|
|
from models.loan_model import LoanApprovalModel
|
|
|
|
def test_trained_model():
|
|
|
|
model_path = 'models/loan_model.joblib'
|
|
if not os.path.exists(model_path):
|
|
print(f"Error: Trained model not found at {model_path}")
|
|
print("Please run 'python src/train_model.py' first to train the model")
|
|
return
|
|
|
|
|
|
print(f"Loading trained model from {model_path}...")
|
|
model = LoanApprovalModel.load(model_path)
|
|
|
|
|
|
print("\nAvailable categories in the trained model:")
|
|
for col, encoder in model.label_encoders.items():
|
|
print(f"{col}: {list(encoder.classes_)}")
|
|
|
|
|
|
good_application = {
|
|
'no_of_dependents': 2,
|
|
'education': ' Graduate',
|
|
'self_employed': ' No',
|
|
'income_annum': 8000000,
|
|
'loan_amount': 15000000,
|
|
'loan_term': 10,
|
|
'cibil_score': 750,
|
|
'residential_assets_value': 10000000,
|
|
'commercial_assets_value': 5000000,
|
|
'luxury_assets_value': 20000000,
|
|
'bank_asset_value': 7000000
|
|
}
|
|
|
|
|
|
risky_application = {
|
|
'no_of_dependents': 5,
|
|
'education': ' Not Graduate',
|
|
'self_employed': ' Yes',
|
|
'income_annum': 2000000,
|
|
'loan_amount': 15000000,
|
|
'loan_term': 20,
|
|
'cibil_score': 350,
|
|
'residential_assets_value': 2000000,
|
|
'commercial_assets_value': 1000000,
|
|
'luxury_assets_value': 5000000,
|
|
'bank_asset_value': 1000000
|
|
}
|
|
|
|
|
|
print("\nTesting with a good application:")
|
|
good_result = model.predict(good_application)
|
|
print(f"Prediction: {'Approved' if good_result['prediction'] == 1 else 'Rejected'}")
|
|
print(f"Confidence: {good_result['probability']:.2%}")
|
|
print(f"Explanation: {good_result['explanation']['text']}")
|
|
|
|
print("\nTop 3 factors:")
|
|
for i, factor in enumerate(good_result['feature_importance'][:3]):
|
|
print(f"{i+1}. {factor['feature']}: {factor['importance']:.4f}")
|
|
|
|
print("\nTesting with a risky application:")
|
|
risky_result = model.predict(risky_application)
|
|
print(f"Prediction: {'Approved' if risky_result['prediction'] == 1 else 'Rejected'}")
|
|
print(f"Confidence: {risky_result['probability']:.2%}")
|
|
print(f"Explanation: {risky_result['explanation']['text']}")
|
|
|
|
print("\nTop 3 factors:")
|
|
for i, factor in enumerate(risky_result['feature_importance'][:3]):
|
|
print(f"{i+1}. {factor['feature']}: {factor['importance']:.4f}")
|
|
|
|
if __name__ == "__main__":
|
|
test_trained_model() |