File size: 1,317 Bytes
b4fbdbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import joblib
import numpy as np
import pandas as pd

#MODEL_PATH = 'model/dt_clf.joblib'  #another method to load the model
model = joblib.load('model/dt_clf.joblib')

FEATURE_NAMES = ['Gender', 'Married', 'Dependents', 'Education',
       'Self_Employed', 'ApplicantIncome', 'CoapplicantIncome', 'LoanAmount',
       'Loan_Amount_Term', 'Credit_History', 'Property_Area']

def preprocess_input(user_input):
    mapping = {
        'Gender': {'Female': 0, 'Male': 1},
        'Married': {"Yes": 1, 'No': 0},
        'Education': {'Graduate': 1, 'Not Graduate': 0},
        'Self_Employed': {'Yes': 1, 'No': 0},
        'Property_Area': {'Rural': 0, "Semiurban": 1, 'Urban': 2},
        'Dependents': {'0':0, '1':1, '2':2, '3':3, '3+':4}
    }

    processed = []
    for feature in FEATURE_NAMES:
        val = user_input[feature]
        if feature in mapping:
            val = mapping[feature][val]
        processed.append(val)

    return np.array(processed).reshape(1, -1)

def make_prediction(user_input):
    try:
        processed_input = preprocess_input(user_input)
        pred = model.predict(processed_input)[0]
        prob = model.predict_proba(processed_input).max()
        return pred, prob
    except Exception as e:
        return f'Error: {str(e)}', 0.0