Tuesday 1 September 2015

mahout: Generating Recommendations for Boolean data sets

Sometimes you need to generate recommendations for input data, which has no preference values. What I mean is, data should be like following, without preferences.

User_id1 item_id1
User_id2 item_id2
User_id3 item_id3
User_id4 item_id4

Above kind of data is called Boolean data, since it has no preference value. To handle such kind of data, we need to select proper similarity algorithms and recommenders.

Choosing similarity algorithm
For this example, i am going to use TanimotoCoefficientSimilarity, is intended for "binary" data sets (preference value doesn't matter here).

Choose Recommender
I am going to use GenericBooleanPrefUserBasedRecommender here. (You can use GenericBooleanPrefItemBasedRecommender, for item based recommendations)
import java.io.File;
import java.util.List;

import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericBooleanPrefUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.TanimotoCoefficientSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

public class RecommenderExample {
 private static String[] books = { "Meet Big Brother",
   "Explore the Universe", "Memoir as metafiction",
   "A child-soldier's story", "Wicked good fun",
   "The 60s kids classic", "A short-form master",
   "Go down the rabbit hole", "Unseated a president",
   "An Irish-American Memoir" };

 private static final int NEIGHBOR_HOOD_SIZE = 5;

 private static String inputFile = "/Users/harikrishna_gurram/customer.csv";

 /**
  * Get Recommender instance for given input file
  * 
  * @param fileName
  * @return
  * @throws Exception
  */
 public static Recommender getRecommender(String fileName) throws Exception {
  /* Create DataModel object from preferences data store in file */
  DataModel model = new FileDataModel(new File(fileName));

  /* Get Person correlation instance from given model */
  // UserSimilarity similarity = new PearsonCorrelationSimilarity(model);

  UserSimilarity similarity = new TanimotoCoefficientSimilarity(model);
  /*
   * Computes a neighborhood consisting of the nearest n users to a given
   * user.
   */
  UserNeighborhood neighborhood = new NearestNUserNeighborhood(
    NEIGHBOR_HOOD_SIZE, similarity, model);

  /* Get Recommender */
  Recommender recommender = new GenericBooleanPrefUserBasedRecommender(
    model, neighborhood, similarity);

  return recommender;
 }

 /**
  * Get noOfRecommendations for given customer
  * 
  * @param recommender
  * @param custId
  * @param noOfRecommendations
  * @return
  * @throws Exception
  */
 public static List<RecommendedItem> getRecommendations(
   Recommender recommender, int custId, int noOfRecommendations)
   throws Exception {
  return recommender.recommend(custId, noOfRecommendations);
 }

 public static void displayRecommendations(int custId,
   List<RecommendedItem> recommendations) {
  System.out.println("Recommendations for customer " + custId + " are:");
  System.out.println("*************************************************");

  for (RecommendedItem recommendation : recommendations) {
   int bookId = (int) recommendation.getItemID();
   System.out.println(bookId + " " + books[bookId - 1]);
  }

  System.out.println("*************************************************");
 }

 public static void main(String args[]) throws Exception {

  Recommender recommender = getRecommender(inputFile);
  List<RecommendedItem> recommendations;

  recommendations = getRecommendations(recommender, 1, 5);
  displayRecommendations(1, recommendations);

  recommendations = getRecommendations(recommender, 2, 5);
  displayRecommendations(2, recommendations);
 }
}


Output

Recommendations for customer 1 are:
*************************************************
4 A child-soldier's story
1 Meet Big Brother
8 Go down the rabbit hole
6 The 60s kids classic
3 Memoir as metafiction
*************************************************
Recommendations for customer 2 are:
*************************************************
9 Unseated a president
*************************************************



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment