Machine Learning as a Service

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.

Simple Example


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

Frame - find probability of default


In [3]:
# Predict `default` probability

Acquire - load historical data


In [4]:
df = pd.read_csv("../data/historical_loan.csv")

Refine - drop NAs


In [5]:
df.dropna(axis=0, inplace=True)

Transform - log scale


In [6]:
df['log_age'] = np.log(df.age)
df['log_income'] = np.log(df.income)

Explore - age, income & default


In [7]:
df.plot.scatter(x='log_age', y='log_income', c='default', alpha=0.25, cmap='viridis')


Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x1107c5400>

Model - Build a tree classifier


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]:
['clf.pkl']

In [10]:
tree.export_graphviz(clf, 
                     out_file="tree.dot", 
                     feature_names=["age", "income"],
                     class_names=["no", "yes"])

Build - the ML API


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


Overwriting simple.py

Deploy - the ML API

Run the following command in your terminal

 cd credit-risk/notebooks/
 firefly simple.predict

Interact - get prediction using API


In [9]:
simple = firefly.Client("http://127.0.0.1:8000")
simple.predict(age=28, amount=10000)


Out[9]:
0.5373423860329777

In [ ]: