Create a Movie Recommendation App with Prediction.io – Setup

Share this article

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.

Frequently Asked Questions (FAQs) about Creating a Movie Recommendation App

What is PredictionIO and why is it important in creating a movie recommendation app?

PredictionIO is an open-source Machine Learning server that allows developers to create predictive features, such as recommendation systems, in their applications. It’s crucial in creating a movie recommendation app because it provides a stack of machine learning algorithms that can be used to analyze user behavior and predict their preferences. This makes it easier to recommend movies that a user is likely to enjoy based on their past viewing habits.

How does a movie recommendation system work?

A movie recommendation system works by analyzing a user’s past behavior and preferences, and then using this data to predict what other movies they might like. This is typically done using machine learning algorithms, which can identify patterns in the user’s behavior and use these patterns to make accurate predictions. The system can also take into account the preferences of similar users, further improving the accuracy of its recommendations.

What are the key components of a movie recommendation app?

The key components of a movie recommendation app include a user interface, a database of movies, a recommendation engine, and a feedback system. The user interface allows users to interact with the app, the database stores information about the movies, the recommendation engine uses machine learning algorithms to generate recommendations, and the feedback system allows users to rate movies and provide feedback, which helps to improve the accuracy of the recommendations.

How can I improve the accuracy of my movie recommendation app?

The accuracy of a movie recommendation app can be improved by using more advanced machine learning algorithms, incorporating more data into the recommendation process, and continually refining the recommendation engine based on user feedback. It’s also important to ensure that the app is user-friendly and easy to navigate, as this can significantly impact the user’s experience and their willingness to use the app.

What challenges might I face when creating a movie recommendation app?

Some of the challenges you might face when creating a movie recommendation app include dealing with large amounts of data, ensuring the accuracy of the recommendations, and creating a user-friendly interface. Additionally, you’ll need to ensure that the app is able to handle a high volume of requests without slowing down or crashing.

How can I ensure that my movie recommendation app is user-friendly?

To ensure that your movie recommendation app is user-friendly, it’s important to focus on the design and functionality of the user interface. The app should be easy to navigate, with clear instructions and intuitive controls. Additionally, the app should be responsive and fast, as users are likely to abandon an app that is slow or unresponsive.

Can I use other programming languages besides Scala to create a movie recommendation app?

Yes, you can use other programming languages to create a movie recommendation app. While this tutorial uses Scala, other languages like Python, Java, or R can also be used. The choice of programming language largely depends on your personal preference and the specific requirements of your project.

How can I test the performance of my movie recommendation app?

You can test the performance of your movie recommendation app by using a variety of testing methods, including unit testing, integration testing, and load testing. Additionally, you can use analytics tools to monitor the app’s performance in real-time and identify any potential issues.

What is the role of machine learning in a movie recommendation app?

Machine learning plays a crucial role in a movie recommendation app. It is used to analyze user behavior and preferences, and to predict what other movies they might like. The more data the machine learning algorithm has to work with, the more accurate its predictions will be.

How can I monetize my movie recommendation app?

There are several ways to monetize a movie recommendation app. You could charge users a subscription fee for access to the app, or you could use in-app advertising. Alternatively, you could offer a free version of the app with basic features, and a premium version with additional features.

Wern AnchetaWern Ancheta
View Author

Wern is a web developer from the Philippines. He loves building things for the web and sharing the things he has learned by writing in his blog. When he's not coding or learning something new, he enjoys watching anime and playing video games.

BrunoSFlightframeworkmachine learningmlmongomongodbOOPHPPHPpredictionpredictionioratingrecommendation
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week