2

In general, Mahout's datamodel contains UserId,ItemId,Prefs in the file, which translates to logged-in/registered users. But mostly we have non-logged-in/anonymous users, viewing our items
e.g. in IMDB you watch movie details without login.
For such cases, I need to implement "Users who viewed this also viewed that" recommendation.
Can anybody suggest, how to achieve this with Mahout?

PS: For my system, for each view I store IP address of user, which can be used for associating with itemId.

mukherjeejayant
  • 153
  • 1
  • 7

1 Answers1

2

You can try using PlusAnonymousUserDataModel to recommend items to anonymous users. This can be done by creating wrapper around your recommender class like this:

public class WithAnonymousRecommender extends Recommender {
         private final PlusAnonymousUserDataModel plusAnonymousModel;

         public WithAnonymousRecommender(DataModel model) throws TasteException, IOException {
              super(new PlusAnonymousUserDataModel(model)); 
              plusAnonymousModel =  (PlusAnonymousUserDataModel) getDataModel();

         }

         public synchronized List<RecommendedItem> recommend( PreferenceArray anonymousUserPrefs, int howMany) throws TasteException {
              plusAnonymousModel.setTempPrefs(anonymousUserPrefs); 
              List<RecommendedItem> recommendations = recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, howMany, null);
              plusAnonymousModel.clearTempPrefs(); 
              return recommendations;
         } 

         //Test it
         public static void main(String[] args) throws Exception { 
                PreferenceArray anonymousPrefs = new GenericUserPreferenceArray(3);
                anonymousPrefs.setUserID(0, PlusAnonymousUserDataModel.TEMP_USER_ID);
                anonymousPrefs.setItemID(0, 123L); anonymousPrefs.setValue(0, 1.0f);                  
                anonymousPrefs.setItemID(1, 123L); anonymousPrefs.setValue(1, 3.0f); 
                anonymousPrefs.setItemID(2, 123L); anonymousPrefs.setValue(2, 2.0f); 
                WithAnonymousRecommender recommender = new WithAnonymousRecommender();
                List<RecommendedItem> recommendations = recommender.recommend(anonymousPrefs, 10); 
                System.out.println(recommendations);
    }
}

You can find more about this in chapter 5.4 in the book Mahout in Action

However, here all anonymous users will be treated as one user. This can be used to recommend items when the user does not have enough watched/liked items. I suggest if you know the IP address of the user to create temporary users for each IP address and collect implicit feedback (clicks, watched movies...) and remove the user after the user exits the site (or have some timeout to remove the user), because using PLusAnonymousUser will not give you personalization.

Dragan Milcevski
  • 776
  • 7
  • 17