How to Build a Recommender with Create ML App

Machine Learning is the part of Artificial Intelligence that is based on the idea that machines can learn from data and can complete a specific task without being explicitly programmed. The main goal of Machine Learning is to improve people’s lives by making machines more intelligent and technology more personal.

The perfect example of combining these two possibilities that Machine Learning provides is a Recommender System. Basically Recommender Systems understand the user’s preferences and provide accurate recommendations based on their personal likes and dislikes.

Recommender Systems are widely used in a variety of sectors such as media recommenders for platforms like Netflix, Apple Music, Hulu, as content recommenders for platforms such Facebook, Instagram, Pinterest and for product recommenders for almost every online shopping platform such as Amazon.

The importance of Recommender Systems is apparent, especially for the personalisation of the user experience and services that use them are more competitive and more likely to succeed in boosting their sales figures. In this article we will provide a brief introduction in the recommender systems and the way Apple is integrating them in its Core ML Framework. Then we will present how to practically train and create a simple Recommender model using Apple’s Technology.

The Recommender Systems

A Recommender Model, in order to work properly, has to be trained with a set of well represented and suitable data to create the user profile. There are two main types of information filtering in Recommender Systems:

  • Collaborative Filtering: this type of filtering is based on past ratings and preferences of users with similar tastes. Basically, it tries to find commonalities among users and when it does, it recommends new items that users with similar preferences had liked in the past. A very good example would be Netflix where if two very similar users have watched movies A,B,C and the first user also watched movie D, the system will recommend to the second user to watch movie D as well.
  • Content-based Filtering: this type of filtering is based on similarities of items themselves. According to the user’s preference for a certain item in the past, the content-based filtering attempts to find other similar items and recommends them to the user. For instance, if a user bought gym clothes in the past, it is likely that next time the suggestions for the user will be sneakers, gym bags and other gym related items.
Illustration of the two main types of information filtering

Recommender System and Apple

Apple at the WWDC 2018 introduced its Machine Learning framework CreateML, where developers could easily train machine learning models with Swift and Xcode. Only one year later at the WWDC 2019 they announced their newest Create ML App, an amazing tool where practically anyone can train machine learning models, without writing a single line of code. In essence, Apple made Machine Learning modelling accessible to everyone while maintaining the simplicity and the performance.

On top of that Apple announced that all these models work offline on the user’s device without lacking in efficiency and speed, a very important feature for the user’s privacy protection. In this way Apple managed to maintain the balance of user personalisation and user privacy, an invaluable attribute for any technology.

We believe that through this project we can demonstrate the power of the Recommender model of Create ML, by illustrating the variety of applications where it can be used as well as the importance of even the most simple personalisation in the overall user experience.

Of course we had to try for ourselves this new interactive way of workflow that the Create ML App provides. Out of the five available domains we chose to create our project with the Tabular Data domain, and specifically with the model type of the Recommender.

The Project

How many times have you gone to a bar and although you are sure of your usual preferences -a classic but basic Mojito- you just want to try something different for a change? And every time you try asking for the waiter’s or the bartender’s choice you end up with a cocktail that you do not really like, right? If only the bartender knew your exact preferences in taste and could recommend you a cocktail that you would surely love!

That gave us the idea of creating a simple Cocktail Recommender which based on your previous preferences on cocktails is able to provide you with accurate and personalised suggestions on new similar cocktails that suit your taste.

With this simple idea the user can now have a more fun, engaging and personalised experience and on top of that they can save on these 8€ they would otherwise spend by trying out a cocktail that they don’t even like.

And this is how our project was born.

The Dataset

In our opinion assembling and editing the dataset in a correct and efficient way is the most challenging and important part of the whole process. A wrongly structured dataset equals a wrongly performing model.

The original idea about the information filtering was a content-based approach, meaning a way of filtering based on the cocktail’s taste that the user prefers. However we had to create a hybrid of content-based and collaborative filtering due to the fact that the Recommender model in the CreateML App is more appropriate for the latter type of filtering.

Illustration of the combination of the two types of filtering for our project

Our first challenge was that, in order to use both filtering techniques, we had to “translate” the data from item-similar to user-similar. We ended up creating our own small dataset where we chose to replace the conventional user of the collaborative filtering with the categories that represent the taste of each user, so that their past cocktail preferences will shape the recommendation, content-based filtering style.

In order to achieve higher accuracy we rated each category of taste from 1 to 5 based on how well each specific cocktail corresponds to the taste. In this example where the ratings do not derive from a particular user, it is crucial to understand what attributes you want to use in your model. For example, in our project training the model based on the alcohol preference of the user would be very vague and inefficient.

The Cocktail Dataset

We created a CSV file with three columns: category, that represents the category of users with similar taste, name, that represents the name of the cocktail and rating, that shows the rating for each cocktail.

The Create ML Recommender Model

Once the CSV file is created, the actual training of the Recommender model begins. To train the model you have to open Xcode and then from the menu in the left upper corner you select Xcode > Open Developer Tool > Create ML, once the Create ML App has launched you select the Recommender Template.

The Recommender Template inside the Create ML App

Next, you have to place as input the CSV file you created -you can even do it with drag and drop- and you can see both the number of items examined but at the same time the total number of examples. You then have to select the name you are using in your file for the user, the item and optionally the rating.

When you are all settled press the training button and your model will be ready to use

Last step to actually test your model, is to go in the input panel and then add a live user in order to try different items and validate the recommendations you are getting.

You can see that when you add a user, you have to select an input item from the drop down menu and set a rating -which must be above zero- and based on your selection all the predictions appear on the space on the right. In our example if the user selects a cocktail and rates it, they will get a recommendation based on their preferred taste.

The Create ML Model in Xcode

Once the process of creating and training the model has been completed, you can just drag and drop the complete model from the output section of the Create ML and you can integrate it in your final Xcode project.

With our Create ML Model we managed to create our simple app in Xcode. What we had to do was to integrate the existing model in our Xcode and actually build our application. If you want to find out more about how to integrate the model in Xcode, the full source code of our Cocktail Recommender Application is available in this public GitLab Repository.

The Conclusion

By working on this project we got a small taste of what Apple’s Machine Learning Technology and Recommender Systems offer (pun very much intended). Of course we are aware of the countless possibilities and applications of these technologies and we only hope to explore more.

We saw how important is to collect and edit the data in the most efficient way for your problem and of course how easy and engaging is training your ML models with Apple’s Create ML App.

We very much encourage you to try training your own Recommender models and see for yourselves how the very popular Recommendation System work.

Finally, we would like to thank Pasquale Vittoriosi, whose guidance and support proved to be invaluable in every step of our project.

That’s all folks, we hope you enjoyed reading our article and remember: Drink safe — never leave the next cocktail you are drinking to chance!

The Team: Stefano Di Nunno, Gianna Stylianou, Ivan Tilev