How to build an API for a machine learning model in 5 minutes using Flask

2 min read
Curated from kdnuggets.com →

Flask is a micro web framework written in Python. It can create a REST API that allows you to send data, and receive a prediction as a response.

As a data scientist consultant, I want to make impact with my machine learning models. However, this is easier said than done. When starting a new project, it starts with playing around with the data in a Jupyter notebook. Once you’ve got a full understanding of what data you’re dealing with and have aligned with the client on what steps to take, one of the outcomes can be to create a predictive model.

You get excited and go back to your notebook to make the best model possible. The model and the results are presented and everyone is happy. The client wants to run the model in their infrastructure to test if they can really create the expected impact. Also, when people can use the model, you get the input necessary to improve it step by step. But how can we quickly do this, given that the client has some complicated infrastructure that you might not be familiar with?

Get the AI & data signal, daily.

335k+ subscribers read this every morning. One email, both newsletters. Unsubscribe anytime.

For this purpose you need a tool that can fit in their complicated infrastructure, preferably in a language that you’re familiar with. This is where you can use Flask. Flask is a micro web framework written in Python. It can create a REST API that allows you to send data, and receive a prediction as a response.

  Let me show you how this works. For the purpose of demonstration, I will train a simple DecisionTreeClassifier model on an example dataset which can be loaded from the scikit-learn package.

Once the client is happy with the model you have created, you can save it as pickle file. You can then open this pickle file later and call the function  to get a prediction for new input data. This is exactly what we will do in Flask.

Continue Reading

Enjoyed this summary? Read the complete article at the source:

Continue at kdnuggets.com →