使用Mahout为布尔型数据生成推荐内容

Boolean data Sets
                Input data set that doesn’t have a preference value, ie input data set would be of the format UserId1,ItemId1
UserId2,ItemId2
Here it’d based on some data where an user either likes an item or he doesn’t, there is no preference value associated with this.
 
                When we use Boolean data sets we need to appropriately choose the Similarity algorithms and Recommenders
 
Similarity Algorithms
                For Boolean data sets we can either go in for Tanimoto Coefficient Similarity or Log Likelihood Similarity
 
Recommender
                We need to use Generic Boolean Pref User Based Recommender or Generic Boolean Pref Item Based Recommender
 
                Sample codes for generating User based and Item Based recommendations are given below
 
Used Based Recommender for Boolean Data Sets
 
import  java.io.File;
import  java.io.IOException;
import  java.util.List;
 
import  org.apache.mahout.cf.taste.common.TasteException;
import  org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import  org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import  org.apache.mahout.cf.taste.impl.recommender.GenericBooleanPrefUserBasedRecommender;
import  org.apache.mahout.cf.taste.impl.similarity.TanimotoCoefficientSimilarity;
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  UserRecommender {
     
       public   static   void  main(String args[])
      {
             // specifying the user id to which the recommendations have to be generated for
             int  userId=510;
           
             //specifying the number of recommendations to be generated
             int  noOfRecommendations=5;
           
             //specifying theNeighborhood size
             double  thresholdValue=0.7;
           
             try
            {
                   // Data model created to accept the input file
                  FileDataModel dataModel =  new  FileDataModel( new File( "D://input.txt" ));
                 
                   /*TanimotoCoefficientSimilarity is intended for "binary" data sets
                  where a user either expresses a generic "yes" preference for an item or has no preference.*/
                  UserSimilarity userSimilarity =  new  TanimotoCoefficientSimilarity(dataModel);
                 
                   /*ThresholdUserNeighborhood is preferred in situations where we go in for a
                   similarity measure between neighbors and not any number*/
                  UserNeighborhood neighborhood = new  ThresholdUserNeighborhood(thresholdValue, userSimilarity, dataModel);
                 
                   /*GenericBooleanPrefUserBasedRecommender is appropriate for use when no notion
                  of preference value exists in the data. */
                  Recommender recommender = new  GenericBooleanPrefUserBasedRecommender(dataModel, neighborhood, userSimilarity);
                 
                   //calling the recommend method to generate recommendations
                  List<RecommendedItem> recommendations =recommender.recommend(userId, noOfRecommendations);
           
                   //
                   for  (RecommendedItem recommendedItem : recommendations)
                        System. out .println(recommendedItem.getItemID());
            }
             catch  (IOException e) {
                   //  TODO  Auto-generated catch block
                  e.printStackTrace();
            }  catch  (TasteException e) {
                   //  TODO  Auto-generated catch block
                  e.printStackTrace();
            }
           
                 
      }
 
}
 
Item Based Recommender for Boolean Data Sets
 
import  java.io.File;
import  java.io.IOException;
import  java.util.List;
 
import  org.apache.mahout.cf.taste.common.TasteException;
import  org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import  org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import  org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity;
import  org.apache.mahout.cf.taste.recommender.ItemBasedRecommender;
import  org.apache.mahout.cf.taste.recommender.RecommendedItem;
import  org.apache.mahout.cf.taste.similarity.ItemSimilarity;
 
public   class  ItemRecommender {
     
       public   static   void  main(String args[])
      {
             // specifying the user id to which the recommendations have to be generated for
             int  userId=510;
           
             //specifying the number of recommendations to be generated
             int  noOfRecommendations=5;
           
             try
            {
                   // Data model created to accept the input file
                  FileDataModel dataModel =  new  FileDataModel( new File( "D://input.txt" ));
                 
                   /*Specifies the Similarity algorithm*/
                  ItemSimilarity itemSimilarity =  new  LogLikelihoodSimilarity(dataModel);
                 
                   /*Initalizing the recommender */
                  ItemBasedRecommender recommender = new  GenericItemBasedRecommender(dataModel, itemSimilarity);
                 
                   //calling the recommend method to generate recommendations
                  List<RecommendedItem> recommendations =recommender.recommend(userId, noOfRecommendations);
           
                   //
                   for  (RecommendedItem recommendedItem : recommendations)
                        System. out .println(recommendedItem.getItemID());
            }
             catch  (IOException e) {
                   //  TODO  Auto-generated catch block
                  e.printStackTrace();
            }  catch  (TasteException e) {
                   //  TODO  Auto-generated catch block
                  e.printStackTrace();
            }
           
      }
}

 

你可能感兴趣的:(数据挖掘,Mahout,tech)