Testing the SVD-based model

This recipe brings this chapter on recommendation engines to a close. We use our new nonnegative matrix factorization-based model and take a look at some of the predicted reviews.

How to do it…

The final step in leveraging our model is to access the predicted reviews for a movie based on our model:

        def predict_ranking(self, user, movie):
            uidx = self.users.index(user)
            midx = self.movies.index(movie)
            if self.reviews[uidx, midx] > 0:
                return None
            return self.model[uidx, midx]

How it works…

Computing the ranking is relatively easy; we simply need to look up the index of the user and the index of the movie and look up the predicted rating in our model. This is why it is so essential to save an ordered list of the users and movies in our pickle module; this way, if the data changes (we add users or movies) but the change isn't reflected in our model, an exception is raised. Because models are historical predictions and not sensitive to changes in time, we need to ensure that we continually retrain our model with new data. This method also returns None if we know the ranking of the user (for example, it's not a prediction); we'll leverage this in the next step.

There's more…

To predict the highest-ranked movies, we can leverage the previous function to order the highest predicted rankings for our user:

        import heapq
        from operator import itemgetter
        
            def top_rated(self, user, n=12):
                movies = [(mid, self.predict_ranking(user, mid)) for mid in self.movies]
                return heapq.nlargest(n, movies, key=itemgetter(1))

We can now print out the top-predicted movies that have not been rated by the user:

        >>> rec = Recommender.load('reccod.pickle')
        >>> for item in rec.top_rated(234):
        ...     print "%i: %0.3f" % item
         814: 4.437
        1642: 4.362
        1491: 4.361
        1599: 4.343
        1536: 4.324
        1500: 4.323
        1449: 4.281
        1650: 4.147
        1645: 4.135
        1467: 4.133
        1636: 4.133
        1651: 4.132

It's then simply a matter of using the movie ID to look up the movie in our movies database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.33.157