Finding the best critic for a user
Now that we have two different ways to compute a similarity distance between users, we can determine the best critics for a particular user and see how similar they are to an individual's preferences.
Getting ready
Make sure that you have completed the previous recipes before tackling this one.
How to do it...
Implement a new method for the MovieLens
class, similar_critics()
, which locates the best match for a user:
In [16]: import heapq
...:
...: def similar_critics(self, user, metric='euclidean', n=None):
...: """
...: Finds and ranks similar critics for the user according to the
...: specified distance metric. Returns the top n similar critics.
...: """
...:
...: # Metric jump table
...: metrics = {
...: 'euclidean': self.euclidean_distance,
...: 'pearson': self.pearson_correlation,
...: }
...:
...: distance = metrics.get(metric, None)
...:
...: # Handle problems that might occur
...: if user not in self.reviews:
...: raise KeyError("Unknown user, '%s'....