Mahout Recommendation Engine Without Using Preferences

Posted By : Abhimanyu Singh | 31-Jul-2013

Mahout recommendation engine

In my previous blog,we have seen that recommendation engine when preferences is available. Now consider website IMDB movie website whether people rate movie . Here creating a recommendation engine is possible using my previous blog . But what if you don’t have preference , you have only people details of the movies they have watched online . Now how we could recommend him a new movie .

Here we can use two User based similarity algorithm

  • TanimotoCoefficientSimilarity : This is based on Tanimoto coefficient . It is the number of items that two users express some preference for,divided by number of items that either user expresses some preference for.

  • LogLikelihoodSimilarity : This similarity metrics attempts to determine just how strongly unlikely it is that the two users have no resemblance in their tastes .the more unlikely the more similar they should be . There is 1000 of movies , now two user say x and user y of IMDB have shown some interest in move A and B . And each of them have watched 100 movies online , That doesn’t means they have same interest , as out of 200 hundred movies they have 2 common movies .

The code is similar to my previous code but the difference is that I have removed preference value from file .


package com.oodles;

import org.apache.mahout.cf.taste.impl.model.file.*;
import org.apache.mahout.cf.taste.impl.neighborhood.*;
import org.apache.mahout.cf.taste.impl.recommender.*;
import org.apache.mahout.cf.taste.impl.similarity.*;
import org.apache.mahout.cf.taste.model.*;
import org.apache.mahout.cf.taste.neighborhood.*;
import org.apache.mahout.cf.taste.recommender.*;
import org.apache.mahout.cf.taste.similarity.*;
import java.io.*;
import java.util.*;

class RecommenderWithoutPreference {

  private RecommenderWithoutPreference() {
  }

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

    DataModel model = new FileDataModel(new File("mydata.csv"));  //load data from file needed for computation
    UserSimilarity similarity = new LogLikelihoodSimilarity(model); //log likelihood similarity will be used for making recommendation .
/*To use TanimotoCoefficientSimilarity replace “LogLikelihoodSimilarity” with TanimotoCoefficientSimilarity”.
UserSimilarity implementation provides how similar two two users are using LoglikehoodSimilarity */
    UserNeighborhood neighborhood =  new NearestNUserNeighborhood(2, similarity, model);  //Define a group of user most similar to a given user . 2 define a group of 2 user having most similar preference 

    Recommender recommender = new GenericUserBasedRecommender(  model, neighborhood, similarity); // creates a recommendation engine 

    List>RecommendedItem<recommendations =   recommender.recommend(4, 1); 
/*one recommendation for user with ID 4 . In Mahout it always take Integer value i.e It will always take userId and number of item to be recommended */

    for (RecommendedItem recommendation : recommendations) {
      System.out.println(recommendation);
    }

  }

}

Here is data for mydata.csv file. It contains data in format [userID,ItemID of item accessed]

1,101
1,102
1,103
2,101
2,102
2,103
2,104
3,101
3,104
3,105
3,107
4,101
4,103
4,104
4,106
5,101
5,102
5,103
5,104
5,105
5,106

The output we will get is item 102 with preference value 2.7713852


RecommendedItem[item:102, value:2.7713852]

Thanks

Abhimanyu

About Author

Author Image
Abhimanyu Singh

Abhimanyu is an seasoned technologist . He always keeps himself ahead in embracing and adapting new technologies/frameworks to solve business problems. He specialise in Blockchain technology , Video Content Management & enterprise software .

Request for Proposal

Name is required

Comment is required

Sending message..