Data science is too cool to be left in a notebook. We need some way to make models useful for, let’s say, web applications, but how can we do so? Today we’ll train a simple machine learning model and deploy it with Flask, and then make predictions with Python’s requests library.

If you’re more of a video person, or just want to reinforce your knowledge, feel free to watch our video on the topic. Source code is included:

Why should I care about deployment? Good question. Let me answer it with another one: what’s the purpose of your model if you’re not going to use it? Or at least make someone else use it? Can’t think of any reason? Good, me neither.

What is Flask? Flask is a microframework for developing web applications. We won’t use it for that purpose, but we’ll use Flask-RESTful to expose certain functionalities of our model.

Okay, but what is requests? It’s just another plain simple library that communicates with REST APIs.

Ugh, what’s a REST API? REST, or Representational State Transfer — whichever comes more natural to you — is a set of styles and constraints used for creating web services. Once a Restful API is live, we can make a request to a certain endpoint with certain parameters — that’s all you should know for now. Doing so executes some backend logic — making a prediction in our case.

Here’s the directory structure (feel free to tweak):

The root directory is called mldeploy, and inside we have two more folders: modeler and models. The first one contains modeler.py file (Python created the other two), and the latter will contain a saved model, once we do the training.

Finally, app.py will tie all the things together and will use Flask and Flask-RESTful for exposing the predictive part.

Let’s now make a stupidly simple classifier.

#machine-learning #programming #python #towards-data-science #data-science

Let’s Deploy a Machine Learning Model
1.25 GEEK