When Food Delivery Apps Meet Machine Learning: A Tutorial with Sample Code
23 Sep 2013

UseCase-FoodDelivery

Online food ordering startups are changing the way the food industry works by making it easier and more convenient than ever to order food online. Such services enable users to select and order a wealth of meal choices from multiple restaurants, either on the web or a mobile app, for takeout and delivery. A few examples include SpoonRocket,  Seamless, HastyJust Eat, Goldbely, OrderUp, FoodPanda and GrubHub.

By their very nature, online food ordering services obtain rich transactional data related to customers’ orders, which reflect their preferences. With all this data collected, why not try and use it to improve the users’ ordering experience? If that sounds like an expensive, time consuming, and calorie-burning undertaking to you, then read on – because this is where PredictionIO comes in.

In this tutorial, we’ll take a closer look at how PredictionIO can help your app personally provide suggestions to users on what they should order.

 

Analyzing the Use Case

One aspect about the food delivery service industry is that both restaurants and customers are quite regional. This means that some food delivery services serve only a few cities, or perhaps only a single city, and that user behaviors often vary city by city.

Knowing about such differences is important because: 1) you want to only recommend meals to users that can actually be delivered to where they live; and 2) this may necessitate the need for a different predictive model for each city.

With this in mind, one possible solution is to create an independent prediction engine for each city, something that we can quickly do with PredictionIO. This would give us the option to select different algorithm parameters, and even different algorithms, for each city.

 

Data Design

With an understanding of the use case, it’s time to turn our attention to modeling. PredictionIO relies upon three core types of data: users, items, and actions. Let’s look at how each of these three data types can be used to model an online food ordering service.

User
In the case of an online food ordering service, a “user” is a “customer” who is making an order through a web or mobile app. A “user” record requires a user id (uid) string, which is used to uniquely identify a particular user. You are free to assign any value, but it would be more convenient to use the uids which correspond to those already stored in your app database.

Item
An “item”, like a “user”, requires an item id (iid) string to be uniquely identifiable. The term “item” is broad, because it can represent any object in your application that users interact with. In the case of online food ordering services, an “item” most likely represents a meal.

Optionally, an item record can contain one or more category type IDs which are used for classifying the item. In our example below, we’ll use two categories: the city where the meal can be delivered to, and the type of food ordered, such as sushi or pizza. So, for a pizza meal that can be delivered to San Francisco and New York City, it may come with category type IDs “SF”, “NY” and “Pizza”.

Action
The final core data type to consider is an “action” which, as the name suggests, refers to the action by which users interact with an item. PredictionIO has built-in support for common actions such as like, dislike, rate, view, view details, and conversion. In addition to this, you may also add your own custom actions. In our example, we will just make use of three built-in types:

  • View action – used when a user browses the information of a meal.
  • Conversion action – used when a user orders a meal and completes payment.
  • Rate action – used when a user gives a rating to a meal (e.g. on a scale of 1 to 5, 5 being the best).

 

Personalized Meal Recommendation with PredictionIO

Building this feature with PredictionIO is straightforward:

Step 1: Install PredictionIO Server

Follow the instructions to install PredictionIO.

When the installation is complete, go to the PredictionIO Admin Panel to add a new application (app) for the service you are building. You will then obtain an App Key, which is needed later when you integrate the PredictionIO SDK.

Step 2: Install the SDK in Your Development Language

PredictionIO provides SDK in various languages. Ruby is used in this example. Execute the following shell command. This will install the SDK package on your machine:

Step 3: Integrate the PredictionIO SDK into Your Application for Importing Data

Now you have finished the integration which handles the data import.

Step 4: Create an Item Recommendation Engine

To give personalized meal recommendations to every user, you need an Item Recommendation Engine. Remember, though, we want to have independent predictive models for every city the food delivery service covers. Go to PredictionIO Admin Panel, add an Item Recommendation Engine for each city. Specify an engine name that will be used in your code to retrieve prediction results from this engine (e.g. itemrec-sf, itemrec-ny, etc). The default algorithm will be deployed automatically but you can fine-tune it later, alternatively, there are a number different of algorithms to choose from built-in to PredictionIO.

When the engine status changes to “Running”, it means the prediction results are ready.

Step 5: Query Prediction Results.

By adding the above code, you have fully integrated PredictionIO into your application, and have built a new feature of providing personalized meal recommendations for your users!

 

Last word

This tutorial has shown you how to build more intelligence into an online food ordering service or app using PredictionIO. By using data the system can learn to provide a more personalized ordering experience for customers. They are presented with more relevant (and hopefully delicious) meal choices which reflect their preferences and are intelligently predicted.

One final note to keep in mind is that the use of PredictionIO doesn’t have to end with just providing meal recommendations to customers. For instance, you might also add “People who buy this meal also buy….” suggestions by using the Item Similarity Engine with just a few lines of code:

Once PredictionIO is hooked to your app, you will be able to predict user preferences, and this can open up a number of possibilities to make your app smarter than ever! Download PredictionIO and try it yourself.