Sie sind auf Seite 1von 8

When Food Deliveri Apps Meet Machine Learning A Tutorial With Sample Code

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, Hasty, ust !at, "oldbely, Order#p, $ood%anda and "rubHub. &y their very nature, online food ordering services obtain rich transactional data related to customers' orders, which reflect their preferences. (ith all this data collected, why not try and use it to improve the users' ordering experience) *f that sounds like an expensive, time consuming, and calorie+burning undertaking to you, then read on , because this is where %rediction*O comes in. *n this tutorial, we'll take a closer look at how %rediction*O 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 -uite regional. .his 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. /nowing about such differences is important because0 12 you want to only recommend meals to users that can actually be delivered to where they live3 and 42 this may necessitate the need for a different predictive model for each city. (ith this in mind, one possible solution is to create an independent prediction engine for each city, something that we can -uickly do with %rediction*O. .his would give us the option to select different algorithm parameters, and even different algorithms, for each city.

Data Design (ith an understanding of the use case, it's time to turn our attention to modeling. %rediction*O relies upon three core types of data0 users, items, and actions. 5et's look at how each of these three data types can be used to model an online food ordering service. #ser *n the case of an online food ordering service, a 6user7 is a 6customer7 who is making an order through a web or mobile app. A 6user7 record re-uires a user id 8uid2 string, which is used to uni-uely identify a particular user. 9ou 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. *tem An 6item7, like a 6user7, re-uires an item id 8iid2 string to be uni-uely identifiable. .he term 6item7 is broad, because it can represent any ob:ect in your application that users interact with. *n the case of online food ordering services, an 6item7 most likely represents a meal. Optionally, an item record can contain one or more category type *;s which are used for classifying the item. *n our example below, we'll use two categories0 the city where the meal can be delivered to, and the type of food ordered, such as sushi or pi<<a. So, for a pi<<a meal that can be delivered to San $rancisco and =ew 9ork >ity, it may come with category type *;s 6S$7, 6=97 and 6%i<<a7. Action .he final core data type to consider is an 6action7 which, as the name suggests, refers to the action by which users interact with an item. %rediction*O has built+in support for common actions such as like, dislike, rate, view, view details, and conversion. *n addition to this, you may also add your own custom actions. *n our example, we will :ust make use of three built+ in types0

?iew action , used when a user browses the information of a meal. >onversion action , used when a user orders a meal and completes payment. Rate action , used when a user gives a rating to a meal 8e.g. on a scale of 1 to @, @ being the best2.

Persoanlized Meal ecommendation !ith Prediction"# &uilding this feature with %rediction*O is straightforward0 Step $% *nstall %rediction*O Server $ollow the instructions to install %rediction*O. (hen the installation is complete, go to the %rediction*O Admin %anel to add a new application 8app2 for the service you are building. 9ou will then obtain an App /ey, which is needed later when you integrate the %rediction*O S;/. 4

Step &% *nstall the S;/ in 9our ;evelopment 5anguage %rediction*O provides S;/ in various languages. Ruby is used in this example. !xecute the following shell command. .his will install the S;/ package on your machine0
gem install predictionio-0.6.0.gem

Step '% *ntegrate the %rediction*O S;/ into 9our Application for *mporting ;ata
require "PredictionIO" # In the beginning of your application, you need to instantiate the PredictionIO client ob ect. # !ou need this client ob ect to import data into PredictionIO and retrie"e prediction results. # #dd this line to the place $here you do initiali%ation& client ' PredictionIO&&(lient.ne$)"your #pp *ey"+ # ,hen add the follo$ing lines to your codes $hich handle the corresponding application logic& # -hen a ne$ user registers& begin result ' client.create.user)"the ne$ user I/"+ rescue 0ser1ot(reated2rror '3 e log.error)e+ end # -hen a ne$ meal is added& begin result ' client.create.item)"name of ne$ meal", 4"category type I/ 5", "category type I/ 6"7+ rescue Item1ot(reated2rror '3 e log.error)e+ end # -hen your user logs in& client.identify)"the logged-in user I/"+ # -hen the logged-in user clic8s or "ie$s a meal& begin result ' client.record.action.on.item)""ie$", "the "ie$ed meal I/"+ rescue 06I#ction1ot(reated2rror '3 e log.error)e+ end # -hen the logged-in user orders a meal and pay& # use built-in action "con"ersion" to represent the "order and pay" action begin result ' client.record.action.on.item)"con"ersion", "ordered meal I/"+ rescue 06I#ction1ot(reated2rror '3 e log.error)e+ end # -hen the logged-in user rates a meal& # the "ariable rating stores the rating "alue )from 5 to 9 + begin

result ' client.record.action.on.item)"rate", "rated meal I/", "pio.rate" '3 rating+ rescue 06I#ction1ot(reated2rror '3 e log.error)e+ end

=ow you have finished the integration which handles the data import. Step (% >reate an *tem Recommendation !ngine .o give personali<ed meal recommendations to every user, you need an "tem ecommendation )ngine. Remember, though, we want to have independent predictive models for every city the food delivery service covers. "o to %rediction*O Admin %anel, add an *tem Recommendation !ngine for each city. Specify an engine name that will be used in your code to retrieve prediction results from this engine 8e.g. itemrec+sf, itemrec+ny, etc2. .he 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 %rediction*O. (hen the engine status changes to 6Running7, it means the prediction results are ready. Step *% Buery %rediction Results.

# !ou need to specify the name of the engine from $hich you $ant to retrie"e the prediction results. # #dd the follo$ing code to display recommended meals to the user after the user has logged in& begin # :etrie"e top 9 recommended meals from the item recommendation engine. # #ssuming client.identify)"user id"+ is already called $hen the user logged in. iids ' client.get.itemrec.top.n)"the engine name", 9+ # list of recommended meal I/s rescue Item:ec1ot;ound2rror '3 e iids ' get.default.iids.list)9+ # default beha"ior $hen there is no prediction result, e.g. recommends ne$est meals log.error)e+ # log the error for debugging or analysis later end display.meals)iids+ # display meals to the user

&y adding the above code, you have fully integrated %rediction*O into your application, and have built a new feature of providing personali<ed meal recommendations for your usersC

Last !ord

.his tutorial has shown you how to build more intelligence into an online food ordering service or app using %rediction*O. &y using data the system can learn to provide a more personali<ed ordering experience for customers. .hey are presented with more relevant 8and hopefully delicious2 meal choices which reflect their preferences and are intelligently predicted. One final note to keep in mind is that the use of %rediction*O doesn't have to end with :ust providing meal recommendations to customers. $or instance, you might also add 6%eople who buy this meal also buyE.7 suggestions by using the "tem Similarity )ngine with :ust a few lines of code0
# !ou need to specify the name of the engine from $hich you $ant to retrie"e the prediction results. # -hen a user "ie$s a meal, you can suggest similar meals to the user by adding the follo$ing& begin # retrei"e top 9 similar courses from the item similarity engine iids ' client.get.itemsim.top.n)"the engine name", "the "ie$ed meal I/", 9+ # list of recommended meal I/s rescue Item<im1ot;ound2rror '3 e iids ' get.default.iids.list)9+ # default beha"ior $hen there is no prediction results, e.g. recommends ne$est meals log.error)e+ # log the error for debugging or analysis later end display.meals)iids+ # display meals to the user

Once %rediction*O 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 everC ;ownload %rediction*O and try it yourself.

+o! to ,uild a estaurant ecommendation )ngine


*n this tutorial, we are building a uni-ue recommendation engine on %rediction*O for a restaurant discovery app. Sign into %rediction*O web admin panel using the administrator account you have created during installation. .hen follow these @ steps0

Step $% Add your App


*n the Applications page, add a new app by giving it a name, e.g. FGy Restaurant AppF, and click Create.

Step &% #-tain an App .ey


>lick Develop on FGy Restaurant AppF, and you will find the App /ey. .his is the information you need when you integrate your app with %rediction*O S;/s later.

Step '% Create the )ngine


>lick Add an )ngine. 9ou will see the available engine types of %rediction*O. *n this example, we want to use F*tem Recommendation !ngineF which can predict user preferences for items. *n our case, restaurants are the items. "ive your new engine a name, e.g. Frestaurant+recF, and click Create. =ow you have a working recommendation engine. 9ou can start using it right awayC *f you can spare another minute with us, see how you can fine+tune this engine in FAd:ust %rediction SettingsF 8Step D2. Otherwise, skip to FStart #sing the !ngineF 8Step @2.

Step (% Ad/ust Prediction Settings 0#ptional1


After your first engine is created, you will arrive at the %rediction Settings page. 1. *tem .ypes Settings Here, you can define which types of items, i.e. *tem .ypes, this engine should handle. (ith our example, we may assign a single item type FrestaurantF to all restaurants. &ut other item types such as FcafeF, FbarF, Ffast+foodF, FcasualF and Ffine+diningF may be assigned to individual restaurants. *f you want to this engine to only handle Ffast+foodF and FcasualF types of restaurants, you should add Ffast+foodF and FcasualF in the *tem .ypes Settings area. &y default, an *tem Recommendation !ngine would H*nclude A55 item typesH. I

4. Recommendation %references Recommendation preferences of different applications vary. $or a newsfeed application or a group buying site, it is more desirable to recommend new items to users3 for our example of restaurant discovery app, you may not always need to recommend the newest restaurants. 9ou can fine+tune this engine in the Recommendation %references area. A. Recommendation "oal 9ou can ad:ust what to optimi<e with this engine in this area.

Step *% Start Using the )ngine


Ruby S;/ is used in examples below.
client ' PredictionIO&&(lient.ne$)=app8ey3+

1. *mport your ;ata *mport your users, items and behaviors data into FGy Restaurant AppF through the A%* key that you have obtained0 Add #ser
client.acreate.user)=username3+

Add *tem 8restaurant2


client.acreate.item)=itemname3, =array.of.item.types3+

Add &ehavior
client.identify)=username3+ client.arecord.action.on.item)>rate>, =itemname3, >pio.rate> '3 =rating.from.5.to.93+

=ote *tem Recommendation !ngine uses previous user behavior data to predict usersF future preferences. o .he data you import into FGy Restaurant AppF will be shared among all engines you create. 4. Retrieve %rediction
o

*tem Recommendation !ngine is trainedJre+trained with new data every hour. .o predict top = restaurants that a user may like0
client.identify)=username3+ client.get.itemrec.top.n)=enginename3, =13+

*tem Recommendation !ngine also supports location+based and item validity scenario.

)2tra Step% Select and Tune Algorithms


An Algorithms tab can be found next to the Prediction Settings tab. .his is the place where you can fine+tune the underlying algorithm of the engine.

Das könnte Ihnen auch gefallen