Tuesday, 18 August 2015

mahout : First Recommender Engine

In this post, I am going to explain simple user based recommender.

Lets say I had following books in my web site.

Book Id
Title
1
Meet Big Brother
2
Explore the Universe
3
Memoir as metafiction
4
A child-soldier's story
5
Wicked good fun
6
The 60s kids classic
7
A short-form master
8
Go down the rabbit hole
9
Unseated a president
10
An Irish-American Memoir

Following is customer details and the books they bought.

customer.csv
1,4,3
1,7,2
1,8,2
1,10,1
2,3,2
2,4,3
2,6,3
2,7,1
2,9,1
3,0,3
3,3,2
3,4,1
3,8,3
3,9,1
4,2,5
4,3,4
4,7,3
4,9,2
5,4,5
5,6,4
5,7,1
5,8,3
6,0,5
6,1,4
6,6,2
6,7,3
6,8,1
7,0,1
7,2,3
7,4,3
7,5,2
7,7,2
7,8,1
8,1,3
8,2,2
8,3,1
8,4,3
8,6,2
8,9,1
8,10,1
9,1,1
9,2,3
9,4,4
9,6,3
9,7,3
9,8,1
10,1,4
10,4,4
10,6,1
10,10,1
11,1,5
11,3,5
11,5,3
11,7,5
11,8,2
12,3,4
12,4,2
12,5,2
12,6,1
12,10,3
13,1,5
13,3,3
13,4,2
13,7,2
13,8,1
14,1,3
14,3,3
14,4,3
14,5,1
14,6,1
14,10,1
15,0,3
15,3,2
15,5,3
15,7,5
15,8,3
15,10,2
16,1,1
16,3,1
16,6,2
16,7,1
16,10,1
17,0,1
17,1,2
17,3,1
17,5,2
17,8,1
18,1,3
18,2,2
18,4,3
18,6,1
18,9,2
19,0,2
19,3,3
19,5,1
19,7,1
19,9,5
20,1,3
20,5,3
20,7,3

20,7,3
Means Customer20 bought book 7 and rated it as 3. 1 is low rating 5 is high rating.


Now my task is, I want to recommend some books to my customers. Following application recommends books for a customer. Comments in the program are self-explanatory.
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.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
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);

  /*
   * 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 GenericUserBasedRecommender(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:
*************************************************
1 Meet Big Brother
6 The 60s kids classic
3 Memoir as metafiction
9 Unseated a president
*************************************************
Recommendations for customer 2 are:
*************************************************
2 Explore the Universe
8 Go down the rabbit hole
10 An Irish-American Memoir
*************************************************





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment