This is a starter example to illustrate all the steps in building an ML service.
We will predict the probability of loan default given two variables - age and income.
In [2]:
#Load the libraries and configuration
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn import tree
from sklearn.externals import joblib
import firefly
In [3]:
# Predict `default` probability
In [4]:
df = pd.read_csv("../data/historical_loan.csv")
In [5]:
df.dropna(axis=0, inplace=True)
In [6]:
df['log_age'] = np.log(df.age)
df['log_income'] = np.log(df.income)
In [7]:
df.plot.scatter(x='log_age', y='log_income', c='default', alpha=0.25, cmap='viridis')
Out[7]:
In [8]:
X = df.loc[:,('age', 'income')]
y = df.loc[:,'default']
clf = tree.DecisionTreeClassifier(max_depth=10).fit(X,y)
joblib.dump(clf, "clf.pkl")
Out[8]:
In [10]:
tree.export_graphviz(clf,
out_file="tree.dot",
feature_names=["age", "income"],
class_names=["no", "yes"])
In [8]:
%%file simple.py
import numpy as np
from sklearn.externals import joblib
model = joblib.load("clf.pkl")
def predict(age, amount):
features = [age, amount]
prob0, prob1 = model.predict_proba([features])[0]
return prob1
Run the following command in your terminal
cd credit-risk/notebooks/
firefly simple.predict
In [9]:
simple = firefly.Client("http://127.0.0.1:8000")
simple.predict(age=28, amount=10000)
Out[9]:
In [ ]: