Source: maxpixel.net

Part two of my data science for startups series focused on Python.

In order for data scientists to be effective at a startup, they need to be able to build services that other teams can use, or that products can use directly. For example, instead of just defining a model for predicting user churn, a data scientist should be able to set up an endpoint that provides a real-time prediction for the likelihood of a player to churn. Essentially, the goal is to provide a model as a service, or function call that products can use directly.

Given the small size of a startup, it’s useful for a data scientist to be able to stand up and support an endpoint without needing engineering support. One of the ways that teams can accomplish this functionality is using services such as AWS Lambda, or GCP’s cloud functions. These are often referred to as serverless computing, but another way of viewing them is as functions as a service. Tools like AWS lambda enable data science teams to set up services that can be customer facing, while minimIzing the overhead involved in supporting a live service.

I covered some alternative approaches to these services in my model production post, which discussed how to use Jetty to set up a model prediction as a web endpoint. The main issue with this approach is that the data science team now needs to maintain a web server, which may not be part of the team’s expertise. I also discussed the use of PubSub for near real-time predictions, but this approach is not suitable for providing an endpoint that requires millisecond latency for generating predictions.

This type of capability, providing model predictions with sub-millisecond latency, can be categorized as providing models as a service. AWS lambda provides a great way of implementing these capabilities, but does require some set up to get working with common ML libraries. The goal of this post is to show how to use AWS lambda to set up an endpoint that can provide model predictions. It can be used with most scikit-learn models that can be serialized using pickle. I first discuss setting up a function exposed on the open web, and then show how to package up sklearn predictions as functions.

This posts builds on the previous AWS setup discussed in my prior post. It assumes that you have an AWS account set up and have assigned a role with S3 access. The full source code for this this tutorial is available on GitHub.

Creating a Test Function

AWS lambda enables teams to write functions that services or web clients can invoke, without needing to set up any infrastructure. It’s called serverless, because teams focus on writing functions rather than building systems. To start, we’ll set up a Hello World function that parses input parameters and returns the parameter as part of the response. For this tutorial, I am focusing on web requests, where the parameters are input as part of a query string, and the response is a web request where the body contains html content. We’ll first set up a test function and then use an API Gateway to expose the function to the open web.

The first step is to log into the AWS console and then drill down into the lambda screen. To start, we’ll create a new function using Python 3.6 and the inline editor, as shown below.

Creating our first lambda function

For simple functions without external dependencies, you can use the “edit code inline” functionality to author your lambda function directly in the AWS web interface. For more complicated functions, we’ll need to write code locally or on an EC2 instance, and then upload the packaged up function.

After creating and saving a function, you should be able to test it using the “Test” button. This will prompt a dialog where you can configure parameters to send to the function, which we’ll leave blank for now, but modify for the next step. Go ahead and save the default configuration and then click “Test”. The result should look like the dialog below.