Mahout基于内存的DataMode 推荐引擎

    xiaoxiao2021-12-15  12

    //注释的部分是基于文件也可以理解为基于日志文件的, 

    //DataModel 可以有很多种,实现abstractDataMode的子类,原则上都可以作为数据源,个人觉得,不管是那种DataMode各自有优缺点  //应该视情况而定,  package com.test.mahout;  import java.util.List;  import org.apache.mahout.cf.taste.impl.common.FastByIDMap;  import org.apache.mahout.cf.taste.impl.model.GenericDataModel;  import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray;  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.model.PreferenceArray;  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;  class RecommenderIntro {      public static void main(String[] args) throws Exception {                      /*    FastByIDMap<PreferenceArray> preferences =new FastByIDMap<PreferenceArray>();       *              PreferenceArray prefsForUser1 = new GenericUserPreferenceArray(10); A                          prefsForUser1.setUserID(0, 1L);              prefsForUser1.setItemID(0, 101L); B              prefsForUser1.setValue(0, 3.0f); B                          prefsForUser1.setItemID(1, 102L);              prefsForUser1.setValue(1, 4.5f);                          … (8 more)              preferences.put(1L, prefsForUser1); C              DataModel model = new GenericDataModel(preferences); D              //A Set up PreferenceArray for user 1              //B Add the first of 10 preferences              //C Attach user 1’s preference*/                  // laod Data File          //        DataModel model = new FileDataModel(new File("C:\\mahout_data.txt"));                FastByIDMap<PreferenceArray> preferences =new FastByIDMap<PreferenceArray>();  /*FastByIDMap是经过优化了的key-value数据结够,在这里用来存储基本的元数据,《mahout in action》书中对这个数据结构做了详细的解释*/                  PreferenceArray prefsForUser1=new GenericUserPreferenceArray(3);//注意这里的数字        // 这里是用来存储一个用户的元数据,这些元数据通常来自日志文件,比如浏览历史,等等,不同的业务场合,它的业务语义应该是不一样          prefsForUser1.setUserID(0, 1);/*个人对这里也感觉到非常模糊,其实这里保存用户ID完全用不着key-value结构,也用不着用一个long型的ID,这里个人觉得是为了以后扩展或者,保持统一的格式,注意用就是了*/                  prefsForUser1.setItemID(0, 101);          prefsForUser1.setValue(0, 5.0f);                  prefsForUser1.setItemID(1, 102);          prefsForUser1.setValue(1, 3.0f);                          prefsForUser1.setItemID(2, 103);          prefsForUser1.setValue(2, 2.5f);                           preferences.put(1l, prefsForUser1);//在这里添加数据                           PreferenceArray prefsForUser2=new GenericUserPreferenceArray(4);                     prefsForUser2.setUserID(0, 2);                     prefsForUser2.setItemID(0, 101);           prefsForUser2.setValue(0, 2.0f);                     prefsForUser2.setItemID(1, 102);           prefsForUser2.setValue(1,2.5f);                     prefsForUser2.setItemID(2, 103);           prefsForUser2.setValue(2,5.0f);                     prefsForUser2.setItemID(3,104);           prefsForUser2.setValue(3,2.0f);                               preferences.put(2l, prefsForUser2);                               PreferenceArray prefsForUser3=new GenericUserPreferenceArray(4);                               prefsForUser3.setUserID(0, 3);                     prefsForUser3.setItemID(0, 101);           prefsForUser3.setValue(0, 2.5f);                     prefsForUser3.setItemID(1, 104);           prefsForUser3.setValue(1, 4.0f);                    prefsForUser3.setItemID(2, 105);           prefsForUser3.setValue(2, 4.5f);                     prefsForUser3.setItemID(3, 107);           prefsForUser3.setValue(3, 5.0f);                     preferences.put(3l, prefsForUser3);                               PreferenceArray prefsForUser4=new GenericUserPreferenceArray(4);                     prefsForUser4.setUserID(0, 4);                     prefsForUser4.setItemID(0,101);           prefsForUser4.setValue(0, 5.0f);                     prefsForUser4.setItemID(1,103);           prefsForUser4.setValue(1, 3.0f);                     prefsForUser4.setItemID(2,104);           prefsForUser4.setValue(2,4.5f);                               prefsForUser4.setItemID(3, 106);           prefsForUser4.setValue(3, 4.0f);                     preferences.put(4l, prefsForUser4);                                         PreferenceArray prefsForUser5=new GenericUserPreferenceArray(6);                     prefsForUser5.setUserID(0, 5);                     prefsForUser5.setItemID(0, 101);           prefsForUser5.setValue(0, 4.0f);                     prefsForUser5.setItemID(1, 102);           prefsForUser5.setValue(1, 3.0f);                     prefsForUser5.setItemID(2, 103);           prefsForUser5.setValue(2, 2.0f);                               prefsForUser5.setItemID(3, 104);           prefsForUser5.setValue(3, 4.0f);                     prefsForUser5.setItemID(4, 105);           prefsForUser5.setValue(4, 3.5f);                     prefsForUser5.setItemID(5, 106);           prefsForUser5.setValue(5, 4.0f);                     preferences.put(5l, prefsForUser5);                    DataModel model=new  GenericDataModel(preferences) ;//DataModel的建立          UserSimilarity similarity = new PearsonCorrelationSimilarity(model);                  UserNeighborhood neighborhood = new NearestNUserNeighborhood(2,similarity, model);          // Create the recommender engine          Recommender recommender = new GenericUserBasedRecommender(model,neighborhood, similarity);          // C For user 1, recommend 1 item          List<RecommendedItem> recommendations = recommender.recommend(1, 1);          for (RecommendedItem recommendation : recommendations) {              System.out.println(recommendation);          }                              }  }
    转载请注明原文地址: https://ju.6miu.com/read-1000007.html

    最新回复(0)