This repository serves primarily as source material for Aish's NUS guest lecture 'Machine Learning in the Real World' for Masters students in IE5105 Modeling for Supply Chain Systems course. The repo can be used as a tutorial to go through the following 8 step process to take a Machine Learning model from proof-of-concept to production:
- Frame the problem and look at the big picture.
- Get the data.
- Explore the data to gain insights.
- Prepare the data to better expose the underlying data patterns to Machine Learning algorithms.
- Explore many different models and short-list the best ones.
- Fine-tune your models and combine them into a great solution.
- Build an API using Flask and Swagger
- Launch, monitor, and maintain your system using Docker and Kubernetes/Heroku
- Share a simplified version of a typical process of developing a Machine Learning solution
- Share some of the techniques, algorithms, technologies involved
- Share key things to keep in mind when developing a Machine Learning solution
- For EDA run
model-training/ Bike Sharing EDA.ipynb
- For feature engineering and model development run
model-training/Feature Engineering & Model Development.ipynb
- For a simple Flask+Swagger API script take a look at
api/prediction_api.py
import pickle
from flask import Flask, request, jsonify
from flasgger import Swagger
import numpy as np
import os
with open('../models/rf_model.pkl', 'rb') as model_file:
model = pickle.load(model_file)
app = Flask(__name__)
swagger = Swagger(app)
port = int(os.environ.get("PORT", 5000))
@app.route('/')
def landing_page():
return "Go to https://bike-pred.herokuapp.com/apidocs", 200
@app.route('/predict')
def predict_bike_demand():
variable_list = ['season', 'holiday', 'workingday', 'weather', 'temp',
'humidity', 'windspeed', 'hour', 'year', 'weekday', 'month']
request_list = [float(request.args.get(variable)) for variable in variable_list]
print(np.array(request_list))
print(np.array(request_list).shape)
print("Predicting!")
prediction = model.predict(np.array(request_list).reshape(1, -1))
# print(prediction)
print("Returning Prediction")
return str(prediction)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)
- We build a simple and lean docker image using
Dockerfile
FROM python:3.6
MAINTAINER aish, aish.prabhat@shopee.com
COPY requirements.txt bike/
WORKDIR bike/
RUN pip install -r requirements.txt
COPY ./models ./models
COPY ./api ./api
WORKDIR api/
EXPOSE 5000
CMD gunicorn wsgi:app
- Run the following command
docker build -t bike .
heroku create <app-name>
heroku container:push web --app <app-name>
heroku container:release web --app <app-name>