PHP
Article

Create a Movie Recommendation App with Prediction.io – Setup

By Wern Ancheta

Create a Movie Recommendation App

In this tutorial, I’m going to walk you through Prediction IO, an open-source machine learning server. It allows you to create applications that could do the following:

  • recommend items (e.g. movies, products, food)
  • predict user behavior
  • identify item similarity
  • rank items

You can pretty much build any machine learning application with ease using Prediction IO. You don’t have to deal with numbers and algorithms and you can just concentrate on building the app itself.

Installation

The tutorial assumes your base OS is Linux. If this is not the case, please consider using Homestead Improved for a standardized and easy to use Vagrant box which gets you up and running with MySQL, PHP 5.6 and Nginx in under five minutes.

The first thing that you need to do is to install Prediction IO. You can download Prediction IO from their downloads page. Once you’ve downloaded it, you can extract it to your preferred installation path. Prediction IO is now installed, but it needs the following dependencies to be installed first before you can use it:

  • Java (Version 6+)
  • MongoDB

You can install these prerequisites through the provided setup script. Navigate to the root of your prediction IO installation path and execute the following command:

bin/setup-vendors.sh

That will install all the dependencies for you.

Running Prediction IO

Once all the dependencies are installed, you can run prediction IO by navigating to the root of your installation path and executing the following command from the terminal:

bin/start-all.sh

Once all the services are running, you can access the prediction IO web interface from port 9000, e.g. http://localhost:9000.

When you’ve opened it up in your browser, you will be greeted with a login screen. There’s no default user in prediction IO so you’ll have to create one first. You can do that by executing bin/users from the root of your prediction IO installation. Executing it will give you 3 choices:

1 - Add a new user
2 - Update email of an existing user
3 - Change password of an existing user

After you create a user, log in. To create a new app, simply click on the ‘Add an app’ button and it will ask you for the app’s name. As we are creating a movie recommendations app, we’re just going to name our app ‘movie_recommendations’.

Once that’s done, click on the ‘develop’ button. From there, you can see the App Key which you can use to make requests to the Prediction IO server. The App key is your typical API token.

movie recommendations app

Next, click on the ‘Add engine’ button. From there you can select one of the three engines that are available. As we are creating a movie recommendation app, select the item recommendation engine by typing in the name that you want to give to the engine. Let’s just name it ‘movie-recommender’, then click on ‘create’.

prediction io default engines

Once the engine is created, Prediction will give you the following interface:

prediction io engine settings

This is where you can customize the settings for your movie recommendation engine. Let’s walk through the settings that are available to us:

  • Item Types Settings – this is where you can add and select the type of items that your engine will recommend. Let’s leave it to include all item types.

  • Training Schedule – this is where you input the training schedule. The term training is used in here because this is where Prediction does its magic. What it basically does is apply the machine learning algorithms to the data that have been gathered so far. The data that Prediction needs to do its magic on are the common user actions in your website. Things like viewing a particular page, liking, disliking, or rating something.

Cron

The default training schedule used by Prediction is 0 0 * * * ?. This means that the training is done hourly. Let’s break it down so it makes sense:

  • 0 – seconds
  • 0 – minutes
      • hours
      • day-of-month
      • month
  • ? – day-of-week

This is what’s called a CronExpression. Having a value of 0 for any of the items above means it will only execute on the first instance of the larger unit of time. So having a 0 value in minutes and seconds and then * for hours means that it will only be executed once per hour. The value for day-of-month is also * which means that it executes every day of the month. Having a * value for the month means that its executed every month. Lastly the ? value for the day-of-week means that there’s no specific value for it. The day-of-week is not an optional field but if we do not want to have any values for it then the only option is to put in ?. This means that the training schedule will depend on the other values that were specified. Putting everything together, the training is done every day on the first minute and first second of every hour. For this engine, we set the training schedule to execute every minute:

0 * * * * ?

Going back to the options that Prediction IO provides for us:

  • Recommendation Preferences – this allows you to prioritize specific recommendation settings. Here are the settings that are available:

    • freshness – this gives priority to newly added items. Set its value to 3.
    • serendipity – this gives priority to surprising discovery. Set its value to 7.
    • unseen items only – this can be one of 2 values: recommend all items, recommend unseen only. Normally you would want to set this to ‘recommend unseen only’ then you set values for the ‘seen actions’ field. But for this app, we’re going to select ‘Recommend any items’.
    • seen actions – the actions that can be considered as ‘seen’ by the user. This field is not required if you have selected ‘Recommend any items’ in the ‘unseen items only field’. The value for this field can be one of the following values: like, dislike, rate, view, conversion. You can read more about these built-in actions in the prediction io data collection page.
    • number of recommendations – the number of recommendations you want to generate for each user.
    • Dedup by Custom Attribute – can be used to specify fields to be used for the recommendation.
  • Recommendation Goal – can be used to prioritize a specific user action as the recommendation goal. For this app we will use like.

The Movie DB API

As we are building a movie recommendation app we need to have a decent list of movies that we can use for our app. And for that, we will use the movie DB API.

In order to get access to their API you first have to sign up to their website. Log into your account once you’re done signing up. On your account page, just click on the ‘API’ link. You will then be prompted to enter your personal information along with the type of use. Once you’re done, you will get an email containing the API key that you can use to perform requests.

Building the App

Before we move on to actually building the app, I’ll provide you with an overview.
The app should be as simple as possible so we’ll only implement the features that are absolutely essential:

  • learning phase – this is the part where the app will randomly pick a specific movie from the database and show it to the user. We will then ask the user for an input whether he likes or dislikes the movie.

  • recommendation phase – this is the part where the app would actually recommend movies based on the inputs made by the user in the learning phase.

If you’re more of a visual person, here’s what the learning phase looks like:

learning phase

And here’s the recommendation phase:

recommendation phase

We won’t be implementing a log in system. This means that each time the app is accessed, the user is considered to be a new one.

Installing Dependencies

Now we’re ready to build the app. Add the following dependencies to your composer.json file:

{
  "require": {
     "damel/flight-skeleton": "dev-master",
     "predictionio/predictionio": "~0.6.0",
     "guzzlehttp/guzzle": "4.*"
  }
}

We will be using the Flight Skeleton which is a nice starting point for Flight PHP projects, as it provides an MVC structure to Flight. Flight Skeleton has the Flight PHP framework as its dependency so we don’t have to specify it in our composer.json file.

Another thing we need to install is the Prediction IO PHP SDK. This allows us to talk to the Prediction IO server with ease.

The last thing that we need to install is Guzzle. This allows us to perform HTTP requests with ease, as we will be talking to an API to get some movie data. For more information on Guzzle, see our previous posts here.

Once you’ve added the dependencies to the composer.json file, open up your terminal and execute composer install to install them.

Once all the dependencies are installed, add an autoload directive to your composer.json file. Then declare a class map inside of it. This contains an array of directories that you want composer to autoload. In this case we only want to autoload the controllers directory so that all of our controllers will get loaded automatically:

"autoload": {
  "classmap": [
    "controllers"
  ]
}

After that, execute the following command on your terminal and let composer do the autoloading work for you:

composer dump-autoload

Next, create an .htaccess file in the root of the project directory and add the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

This rewrites the URL for us so we don’t have to put in index.php every time we need to access a specific route. If you’re using Nginx, see here.

Once that’s done, we can begin coding the app. The first thing that you need to do is create an index.php file inside the root of the project directory. This will serve as the main file of the app. This is where we will map the methods we need to use throughout the app, the routes, and overall initialization of the app.

Conclusion

In this part of the series, we’ve learned about the basics of Prediction IO, how we can set it up and its dependencies. We have also taken a look at how to tweak some of its settings for it to produce more relevant recommendations. Stay tuned for the next part where we will actually start building the movie recommendation app.

  • http://www.tinorueb.de Tino Rüb

    First ty for this nice intro. Just note: The Homestead Improved link is broken. A faster way to setup prediction.io with vagrant you can find here: http://docs.prediction.io/current/installation/install-predictionio-with-virtualbox-vagrant.html

    • http://www.bitfalls.com/ Bruno Skvorc

      Thank you, link fixed, sorry about that!

  • Julien iZi-WEB

    Do you guys know how to deal with the new 0.8 version? the vagrant setup page is not available anymore :-(

  • kenji

    FYI, the code presented here is for the version 0.7 of prediction.io not the current version 0.8.

    The version 0.7 is still available at below:

    http://docs.prediction.io/0.7.0/

    The current version (0.8) doesn’t even have the visual admin screen and all are for the command line execution. It uses spark and elastic search which I have yet understood.

  • Zam

    Do you guys know how to deal with docker-Prediction.io with dropWizard

  • TomTom101

    Very nice tutorial! Can you possibly hint towards which “MongoDB class” you are referring to in part 2? It’s apparently not the PECL implementation and neither the Doctrine SDK. It is missing in the GitHub repo you linked to. Thanks a bunch!

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in PHP, once a week, for free.