-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathloan_default_api.py
24 lines (19 loc) · 940 Bytes
/
loan_default_api.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
"""Service to expose the credit risk model as an API.
"""
from sklearn.externals import joblib
# read the encoders and the model
grade_encoder = joblib.load("le_grade.pkl")
ownership_encoder = joblib.load("le_ownership.pkl")
model = joblib.load("loan_default_model.pkl")
def predict(amount, years, age, ownership, income, grade):
"""Returns the probablity of default for given features.
"""
# encoders work on a vector. Wrapping in a list as we only have a single value
ownership_code = ownership_encoder.transform([ownership])[0]
grade_code = grade_encoder.transform([grade])[0]
# important to pass the features in the same order as we built the model
features = [amount, grade_code, years, ownership_code, income, age]
# probablity for not-defaulting and defaulting
# Again, wrapping in a list as a list of features is expected
p0, p1 = model.predict_proba([features])[0]
return p1